hi ,

i have a script to resize image, it works on normal jpg file which taken by normal camera, but when come to image that took by DSLR camera, it fail to resize.

i had debug the code, it fail when script "imagecreatefromjpeg"

my code as below:

function resizeImage($img, $imgPath, $suffix, $by, $quality)
	{
		// Open the original image.
		[COLOR="Red"]$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original (<em>$imgPath/$img</em>)");[/COLOR]
		list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");

	// Determine new width and height.
	$newWidth = ($width/$by);
	$newHeight = ($height/$by);

	// Resample the image.
	$tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
	imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");

	// Create the new file name.
	$newNameE = explode(".", $img);
	//$newName = ''. $newNameE[0] .''. $suffix .'.'. $newNameE[1] .'';
	//$newName = $impath . "/" . $img . "." . $newNameE[1] ;
	$newName = $impath . "/" . $img ;

	// Save the image.
	imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image");

	// Clean up.
	imagedestroy($original);
	imagedestroy($tempImg);
	return true;
}

Please help to see what is going wrong.
thank you in advance

    Just a guess, but I suspect the images that fail are higher resolution. Thus you're probably exceeding a memory limit when the imagecreate function tries to create its bitmap in memory. The amount of memory it needs is directly proportional to the number of pixels (width times height in pixels). You can try using [man]ini_set/man to change the memory_limit and see if that helps. You can also use [man]getimagesize/man to find out the image's dimensions before you try to load it, and do a little math to figure out how much memory you'll need (width height 4 bytes) and decide whether to go ahead or return an error to the user (asking them to resize it to something smaller before submitting it).

      thank you very much for reply

      the file resolution is big, it was taken by DSLR camera, size is 3MB

      the page is already at HTTP server, is there anyway we can change the ini_set( ) ?

      actually we would like to resize the image by script instead asking user to resize it before upload. just like how facebook did, we try upload the same image to facebook for testing, its works. (although we have no idea how facebook deal with high resolution images.)

      Is there a way to resize by script for high resolution images by php?

      thanks in advance

        You can resize it with the GD functions (such as imagecopyresampled), but the catch is that you need to load both the original and the copy into memory, so it won't avoid any max memory issues. You'll need to either change your PHP config (e.g. in the php.ini file or in a local directory via .htaccess -- if allowed), or else edit the script to change the memory_limit setting in your script.

          Facebook probably use Java or Flash based resizing tools which avoid these problems

          I tell users to resize their images first, using one of the free image resizers (e.g bluefive's program).
          It then uploads much faster as well.

            Another option is to install [man]Imagemagick[/man] and use that. Running in a separate process, its memory requirements aren't counted under PHP's limits.

              thanks for the reply

              i will try to look into imagemagick. will post on the result later

              thank you

                Write a Reply...