note 92597 deleted from function.imagejpeg by cmb
| From: | cmb@php.net | Date: | Sun, 02 Oct 2016 07:54:46 +0000 |
| Subject: | note 92597 deleted from function.imagejpeg by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-206721@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
I wanted a function that could resize an image and store the resulting thumbnail in a variable so I
could manually save it. Here is the function. I basically strung together two snippets, one from
this website (I think) and one by MrCoder here: http://www.webdeveloper.com/forum/showthread.php?t=188298.
<?php
function resizeToVariable($sourceImage,$newHeight,$newWidth,$destImage)
{
list($width,$height) = getimagesize($image);
$img = imagecreatefromjpeg($sourceImage);
// create a new temporary image
$tmp_img = imagecreatetruecolor($newHeight,$newWidth);
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0,$newHeight,$newWidth, $width, $height );
// use output buffering to capture outputted image stream
ob_start();
imagejpeg($tmp_img);
$i = ob_get_clean();
// Save file
$fp = fopen ($destImage,'w');
fwrite ($fp, $i);
fclose ($fp);
}
?>
You can use this for all your image resizing tasks. I for some reason had a problem using imagejpeg
to save the image directly.