Resizing a jpeg... imagejpeg();?

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
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Resizing a jpeg... imagejpeg();?

Post 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);
	}

}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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)
Post Reply