Jump to content

Resize Image & Return Base64 Encoded Data Without Creating TEMP File


HungryMind

Recommended Posts

Hi Guys,

 

I'm resizing an image with "imagecopyresampled".

But i want base64_encode data in return without creating TEM file.

function resizeImage($filename, $new_path, $w, $h){	list($width, $height) = getimagesize($filename);	$new_width = $w;	$new_height = $h;	$image = imagecreatefrompng($filename);	$image_p = imagecreatetruecolor($w,$h);	imagealphablending($image_p, false);	imagesavealpha($image_p, true);	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);	imagepng($image_p, $new_path);	imagedestroy($image);	imagedestroy($image_p);	$ret = base64_encode(file_get_contents($new_path));	return $ret;}

In this code, i'm creating a TEMP file.

But this method is too heavy for me.

imagepng($image_p, 'I DONT WANT to CREATE any TEMP FILE');

 

I tried ob_get_content examples, but i failed :/

 

Please guide.

 

Specially waiting replies from my ever loving teachers: thescientist & justsomeguy :)

Link to comment
Share on other sites

I tried another method:

 

resize.php

header('Content-Type: image/png');$filename = $_GET['filename'];$w = $_GET['w'];$h = $_GET['h'];list($width, $height) = getimagesize($filename);$new_width = $w;$new_height = $h;$image = imagecreatefrompng($filename);$image_p = imagecreatetruecolor($w,$h);imagealphablending($image_p, false);imagesavealpha($image_p, true);imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);imagepng($image_p);imagedestroy($image);imagedestroy($image_p);

but when i'm passing *.php through file_get_contents

It's giving me warning:

 

Warning: file_get_contents(resize.php?filename=1.png&w=50&h=50) [function.file-get-contents]: failed to open stream: No such file or directory in C:Program Fileswampwwwprojectajax.php on line 13

function resizeImage($filename){	$ret = base64_encode(file_get_contents($filename));  This is line no. 13	return $ret;}echo resizeImage('resize.php?filename=1.png&w=50&h=50');
Link to comment
Share on other sites

  • 2 weeks later...

You can use output buffering to output the image and get the contents, or you can just write to a temp file, get the data, and delete the file.

 

Warning: file_get_contents(resize.php?filename=1.png&w=50&h=50)
That is going to get the PHP source code, not the result of executing PHP. And when you do that, the question mark and everything after it is part of the filename. PHP does not populate $_GET unless you are using it through a web server, and accessing the source code of the file using file_get_contents does not cause the web server to execute PHP and pass it that script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...