Page 1 of 1

Wrong result when using imagecopyresized

Posted: Mon Jun 28, 2004 5:25 pm
by webcan
Hey guys:

I have the following code:

Code: Select all

<?php
$logoimage = resizeImage($_FILES["photo"]["tmp_name"], 100, 100, 50);

function resizeImage($srcfile, $width, $height, $compression) { 
    $dst_img = imagecreate($width,$height); 

	$src_img = imagecreatefromJPEG("$srcfile"); 
	imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $width, $height, imagesx($src_img), imagesy($src_img));
	return($dst_img);
}
?>
It is supposed to take an uploaded image, resize it, and then return that image to the variable $logoimage (so that I can save it in a BLOB in a database).

However, what happens when I try to INSERT $logoimage using the following code is, instead of the JPG, I get "Resource id #5":

$query = "INSERT INTO `table` SET `logo` = '$logoimage' WHERE `id` = '555' LIMIT 1;";

I think I am not referring to $logoimage properly. I assume the resizeImage function works properly (I modified something I found elsewhere), but I can't really test it until I can properly insert the JPG into the database.

Any ideas?

Thanks,
Peter.

Posted: Mon Jun 28, 2004 5:32 pm
by feyd
you can use output buffering to grab the image data from [php_man]imagejpeg[/php_man]

Posted: Mon Jun 28, 2004 5:42 pm
by webcan
hmm.. i just looked over the ob_start help page, and i dont understand how it works :(

their example isn't clear to me.

how would i modify my function to use output buffering?

Posted: Mon Jun 28, 2004 7:07 pm
by feyd
you can find what you'll need in the user comments of this page: http://www.php.net/manual/en/function.imagepng.php

Posted: Mon Jun 28, 2004 7:38 pm
by webcan
Ok, well, I tried to integrate it with imageJPEG, but what happens now is the JPG gets output to the browser when imageJPEG is called, it won't return. I don't know if I'm doing the buffer thing correct.

Here is what I changed the fcuntion to:

Code: Select all

<?php
$logoimage = resizeImage($_FILES["photo"]["tmp_name"], 100, 100, 50);

function resizeImage($srcfile, $width, $height, $compression) { 
    $dst_img = imagecreate($width,$height); 

	$src_img = imagecreatefromJPEG("$srcfile"); 
	imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $width, $height, imagesx($src_img), imagesy($src_img));
	ob_start();
	$dst_img = imageJPEG($dst_img);
	return(ob_get_flush());
}
?>

Posted: Tue Jun 29, 2004 10:30 am
by pickle
The only way I've been able to get data from image files is by reading them in from the hard-drive ie: make a file, save it, then use fread() to get the data from it.

Oh ya, and use imagecopyresampled() as opposed to imagecopyresized() if you've got > GD 2.01. The thumbnail looks worlds better.

Posted: Tue Jun 29, 2004 11:07 am
by PrObLeM
but it wont resize a gif... :(

Posted: Tue Jun 29, 2004 11:56 am
by webcan
I was able to get it to work by modifying the function as follows:

Code: Select all

function resizeImage() { 
	$tempfile = "/home/www/mydir/temp/" . rand(10000, 99999);
	$srcfile = $_FILES["photo"]["tmp_name"];
	
	$size = getimagesize($srcfile);
	
	// Calculate proportional size
	$height = ($size[1] / $size[0]) * 200;
	$width = ($size[0] / $size[1]) * $height;
	
    $dst_img = imagecreatetruecolor($width, $height);

	$src_img = imagecreatefromJPEG("$srcfile"); 
	imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, imagesx($src_img), imagesy($src_img));
	imagejpeg($dst_img, $tempfile, 100);
	return($tempfile);
}

feyd | use

Code: Select all

tags.[/color]