I used images as BLOB out of a DB for further processing with Imagick. But now I need to use image-files to do the same thing.
I've some problems to convert my "old" script for using files. I changed it to imageCreateFromJpeg
(for reading source-file) and imagejpeg()
(for writing the image-file). But it doesn't work. But the source-file isn't updated. What am I doing wrong?
$image = ImageCreateFromString(base64_decode($file->source));
$image_width = imagesx($image);
$image_height = imagesy($image);
/* Do some processing
some... ImageCreateTrueColor(...);
ImageCopy(...);
ImageCopyResized(...);
ImageCopyMerge(...);
*/
ob_start();
imagejpeg($image, NULL, 100);
$processed_image = base64_encode(ob_get_contents());
imagedestroy($image);
ob_end_clean();
I changed it to this:
$file = 'path/to/image.jpg';
$image = imageCreateFromJpeg($file); // read from file
$image_width = imagesx($image);
$image_height = imagesy($image);
/* Do some processing
some... ImageCreateTrueColor(...);
ImageCopy(...);
ImageCopyResized(...);
ImageCopyMerge(...);
*/
ob_start();
imagejpeg($image, $file, 100); // write everthing to the same file
imagedestroy($image);
ob_end_clean();
Here is a working example (incl. resize - set your values for destination $width
and $height
):
list($originalWidth, $originalHeight) = getimagesize($file);
$src = imagecreatefromjpeg($file);
$dest = imagecreatetruecolor($width, $height);
ImageCopyResampled($dest, $src, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight);
header('Content-Type: image/jpeg');
ob_start();
imagejpeg($dest);
$size = ob_get_length();
header("Content-Length: " . $size);
ob_end_flush();
imagedestroy($dest);
exit;