note 94079 deleted from function.imagejpeg by danbrown

From: Date: Thu, 15 Oct 2009 14:51:24 +0000
Subject: note 94079 deleted from function.imagejpeg by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-161765@lists.php.net to get a copy of this message
Note Submitter: vesselin dot stoykov at promobile dot bg ---- Re: function resizeImage by someone at anydomain dot com To display more than a single image in the browser, please, call that function from html for every single image you want to see. However, in the function you need to add header() before you call imagejpeg() and exit() instead of "return". Code of "test.php" may look like this: <?php if(isset($_GET['img'])) resizeImage($_GET['img'],123); function resizeImage($originalImage,$new_height){ // Get the original geometry and calculate scales list($width, $height) = getimagesize($originalImage); $new_width = round(($width * $new_height) / $height); // Resize the original image $imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg ($originalImage); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); /* CHANGE HERE: */ header('Content-type: image/jpeg'); imagejpeg($imageResized, null, 100); imageDestroy($imageResized); exit(); } echo <<<END <html><body> <img src="test.php?img=DSC00680.JPG"> <img src="test.php?img=DSC00679.JPG"> </body></html> END; ?> The above works. Slowly. Mind that imagecreatefromjpeg() works slowly for large jpg's (>1MB) and resizing of 10 images may take a long time or even server timeout may occur before the script has terminated. Certainly you'd better use php to scan your "images" directory and make an array of valid file names and then echo them into the html. Even better, in the img url you may use only the serial number of the jpg file from the array instead of its qualified name, e.g.: <img src="test.php?img=1">. Change also the call to resizeImage() accordingly.

« previous php.notes (#161765) next »