Resample a JPG image without writing to disk

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Resample a JPG image without writing to disk

Post by WaldoMonster »

Now I use this code to resample a JPG image.
And afterwards add it to a database.
Is it possible to do this without writing to the hard disk?

Thanks,

Waldo Monster

Code: Select all

<?php
//  +-------------------------------------------------------------------------+
//  | Resample Image                                                                   |
//  +-------------------------------------------------------------------------+
Function ResampleImage($source_image, $destination_image, $size)
{
$src_image = imageCreateFromJpeg($source_image);
$dst_image = imageCreateTrueColor($size, $size);
imageCopyResampled($dst_image, $src_image, 0, 0, 0, 0, $size, $size, imageSX($src_image), imageSY($src_image));
imageJpeg($dst_image, $destination_image, 90);
imageDestroy($src_image);
imageDestroy($dst_image);
}


$source_image = 'D:/test.jpg';
$temp_image   = 'D:/temp.jpg';

ResampleImage($source_image, $temp_image, 200);
$image = file_get_contents($temp_image);
unlink($temp_image);

//Add $image to a database
?>
PAW Projects
Forum Commoner
Posts: 30
Joined: Tue Jun 15, 2004 7:43 am
Contact:

Post by PAW Projects »

How about a function that simply returns the resampled image, instead of writing to disk?

Code: Select all

<?php

Function ResampleImage(&$source_image, $size) {
  $src_image = imageCreateFromJpeg($source_image);
  $dst_image = imageCreateTrueColor($size, $size);
  imageCopyResampled($dst_image, $src_image, 0, 0, 0, 0, $size, $size, imageSX($src_image), imageSY($src_image));
  return $dst_image;
}


$source_image = 'D:/test.jpg';

$img_resampled = ResampleImage($source_image, 200);


//Add $img_resampled to a database
?>
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post by WaldoMonster »

Thanks for the help in combination with the PHP help page I got it working.

Your script returns a raw resampled image.
And I want to add a JPG to the database.
With ob_get_contents(); you can read the output buffer and use that.

Code: Select all

<?php
Function ResampleImage(&$source_image, $size) { 
$src_image = imageCreateFromJpeg($source_image); 
$dst_image = imageCreateTrueColor($size, $size); 
imageCopyResampled($dst_image, $src_image, 0, 0, 0, 0, $size, $size, imageSX($src_image), imageSY($src_image));

ob_start();	                                    //Start buffering the output stream
ImageJpeg($dst_image, NULL, 90);    //Output the image as a file to the output stream
$buffer = ob_get_contents();             //Read the output buffer
ob_end_clean();                               //Clear the buffer

imageDestroy($src_image);
imageDestroy($dst_image);
return $buffer; 
}


$source_image = 'D:/test.jpg'; 

$img_resampled = ResampleImage($source_image, 200); 


//Add $img_resampled to a database 
?>
PS Where is the & symbol for in your PHP script?
Function ResampleImage(&$source_image, $size)

Edit: added imageDestroy to the php script.
Post Reply