Page 1 of 1

Resizing a jpeg... imagejpeg();?

Posted: Sun Aug 07, 2005 8:17 am
by bokehman
I am putting together a function to create a thumbnail image and I am stuck with imagejpeg(). This function either outputs directly to the browser or writes to a file. I don't want either. I want to write the output to MySQL. Anyway here is what I have, which works but most probably imagejpeg() is the wrong function. The output buffering was just a dodge to make the function work but I want to eliminate it.

Here is the portion in question:

Code: Select all

ob_start();
imagejpeg($image_p, NULL, 100);
$image = ob_get_clean();
ob_end_flush();
return($image);


And here is the rest of the code:

Code: Select all

function jpeg_thumbnail($file, $thumb_max_width = 100, $thumb_max_height = 100)
{
	if(file_exists($file)){
	
		$details = getimagesize($file);
		if($details['mime'] != ('image/jpeg' or 'image/png' or 'image/gif')){
			return FALSE;
		}
		
		// Get new dimensions
		list($width, $height) = $details;
		
		if ($width < $height) {
		   $thumb_max_width = ($thumb_max_height / $height) * $width;
		} else {
		   $thumb_max_height = ($thumb_max_width / $width) * $height;
		}
		
		// Resample
		$image_p = imagecreatetruecolor($thumb_max_width, $thumb_max_height);
		if($details['mime'] == 'image/jpeg'){
			$image = imagecreatefromjpeg($file);
		}elseif($details['mime'] == 'image/png'){
			$image = imagecreatefrompng($file);
		}elseif($details['mime'] == 'image/gif'){
			$image = imagecreatefromgif($file);
		}
		imagecopyresampled($image_p, $image, 0, 0, 0, 0, $thumb_max_width, $thumb_max_height, $width, $height);
			
		// Output
		ob_start();
		imagejpeg($image_p, NULL, 100);
		$image = ob_get_clean();
		ob_end_flush();
		return($image);
	}

}

Posted: Sun Aug 07, 2005 8:30 am
by feyd
what's the problem? It looks, overall, fine... the output buffering is required if you want to get a string of the image stream. There's no way around that.

Posted: Sun Aug 07, 2005 8:40 am
by bokehman
Well I was thinking that if I had to use output buffering then maybe imagejpeg() wasn't the right function to have used.

Posted: Sun Aug 07, 2005 2:29 pm
by josh
I would change

Code: Select all

if($details['mime'] == 'image/jpeg'){
            $image = imagecreatefromjpeg($file);
        }elseif($details['mime'] == 'image/png'){
            $image = imagecreatefrompng($file);
        }elseif($details['mime'] == 'image/gif'){
            $image = imagecreatefromgif($file);
        }
To

Code: Select all

switch ($details['mime']) {
   case 'jpeg':
   case 'jpg':
   case 'pjpeg':
      $image = imagecreatefromjpg($file);
   break;
   case 'gif':
     $image = imagecreatefromgif($file);
   break;
   default:
     ob_start();
        include('images/' . basename($file));
    $image = imagecreatefromstring(ob_get_clean());
   break;
}
Notice how jpg's have different mime types, same encoding. Also you'll notice the default part, imagecreatefromstring doesn't need a mime type, it figures out the type all by itself, so if the mime type is not recognized by your script you see if imagecreatefromstring can figure it out.
php manual wrote: These types will be automatically detected if your build of PHP supports them: JPEG, PNG, GIF, WBMP, and GD2.
Also some kind of error checking wouldn't hurt:

Code: Select all

if ($file===false)