note 82118 added to function.imagejpeg
| From: | gcc dot programmer at gmail dot com | Date: | Fri, 28 Mar 2008 05:24:16 +0000 |
| Subject: | note 82118 added to function.imagejpeg | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137654@lists.php.net to get a copy of this message | ||
The following was developed using Mac OSX ( Linux ). The ghostscript call would have to be changed
on a Windows box...
In an application I was developing, I needed to create .jpg thumbnails from .pdf uploaded files.
After some research, I found one easy way to accomplish this was ( SIMPLIFIED ):
<?php
exec("sw/bin/gs -q -dNOPAUSE -dBATCH -sDEVICE=jpeg
-sOutputFile=test.jpg theFile.pdf");
?>
This of course assumes that ghostscript is installed on the server, you have permissions, yada...
Essentially this calls on ghostscript to do the conversion.
So, then, once the new .jpg is created, it needed to be resized to accomodate the UI. Though I
could easily do it from the command line with "convert", I was having issues using this
command from PHP ( it's not my first language ). I found this thread while groping for a
different method, and found an unworkable solution that stimulated me to write the following:
( commented out lines were for debug, but might prove instructional for your own purposes, so I left
them in )
[This function takes in a .jpg file to be converted to a thumbnail, the new width you want the
thumbnail to be, and the name to which you want the thumbnail saved. It then creates the thumbnail]
<?php
function createImage( $upload, $newWidth, $name )
{
if (!$info = getimagesize($upload) )
return false;
//echo "width: " . $info[0] . "<br/>";
//echo "height: " . $info[1] . "<br/>";
//create crude aspect ratio:
$aspect = $info[0] / $info[1];
$newHeight = round( $newWidth/$aspect );
//echo "$newHeight <br/>";
$src = @imagecreatefromjpeg("$upload");
if ( !$src )
return false;
$tmp = @imagecreatetruecolor( $newWidth, $newHeight );
imagecopyresampled( $tmp, $src, 0, 0, 0, 0, $newWidth,
$newHeight, $info[0], $info[1] );
imagejpeg( $tmp, $name, 100 );
imagedestroy( $src );
imagedestroy( $tmp );
return true;
}
?>
$upload = the path to the file you want to make a thumbnail from.
$newWidth = the width of your thumbnails.
$name = the path ( including name ) of the thumbnail you're creating.
If I have test.jpg in my servers root directory, and I want a thumbnail
with a width of 120 pixels, with the name thumb.jpg also to be saved
in the servers root directory, I'd call this function thus:
<?php
createImage("test.jpg", 120, "thumb.jpg");
?>
I hope this makes life easier for somebody.
----
Server IP: 208.69.120.35
Probable Submitter: 24.234.45.166
----
Manual Page -- http://www.php.net/manual/en/function.imagejpeg.php
Edit -- https://master.php.net/note/edit/82118
Del: integrated -- https://master.php.net/note/delete/82118/integrated
Del: useless -- https://master.php.net/note/delete/82118/useless
Del: bad code -- https://master.php.net/note/delete/82118/bad+code
Del: spam -- https://master.php.net/note/delete/82118/spam
Del: non-english -- https://master.php.net/note/delete/82118/non-english
Del: in docs -- https://master.php.net/note/delete/82118/in+docs
Del: other reasons-- https://master.php.net/note/delete/82118
Reject -- https://master.php.net/note/reject/82118
Search -- https://master.php.net/manage/user-notes.php