note 82118 deleted from function.imagejpeg by cmb

From: Date: Sun, 20 Oct 2019 08:58:57 +0000
Subject: note 82118 deleted from function.imagejpeg by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212720@lists.php.net to get a copy of this message
Note Submitter: gcc dot programmer at gmail dot com ---- 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.

« previous php.notes (#212720) next »