Jump to content

[SOLVED] Thumbnails on the fly


bluebyyou

Recommended Posts

I was helped earlier to get this going to create a thumbnail and save it. Now I wanted to go a step further and use it to create the thumbnail on the fly instead of saving it. however instead of outputting an image i get a bunck of jibberish text.

 

I tried calling just the function with the image location as well as calling the function inside the img tag.

 

Is this even possible?

 

Here is the code:

<?php
function thumbnail($image_location){
//Create Thumbnail Image
$image = imagecreatefromjpeg("uploads/$image_location");							// Uploaded file location
$image_width=imagesx($image);													// Get Photo width
$image_height=imagesy($image);													// Get Photo height

// Thumbnail Dimensions
$thumb_width = 228;																// Thumbnail Display Width
$thumb_height = 170;																// Thumbnail Display Height

// Thumbnail Image Quality
$img_quality = 75;																// Image quality for the web is 75 dpi

$height_ratio = round($thumb_height / $image_height, 2);						// Calculate height shrink ratio
$width_ratio = round($thumb_width / $image_width, 2) ;							// Calculate width shrink ratio


if ($image_height <= $thumb_height && $image_width <= $thumb_width) 		
{
$thumbnail_width = $image_width;												//Keep original width		
$thumbnail_height = $image_height;												//Keep original height
}
else if ($image_height > $thumb_height || $image_width > $thumb_width)
{

if ( $image_height > $thumb_height && $image_width < $thumb_width )		
{

$thumbnail_width = $image_width * $height_ratio;							// shrink width 
$thumbnail_height = $image_height * $height_ratio;							// shrink height
}
else if ($image_width > $thumb_width && $image_height < $thumb_height )
{
//echo "here";
$thumbnail_width = $image_width * $width_ratio;								// shrink width 
$thumbnail_height = $image_height * $width_ratio;							// shrink height
}
else //($origh > $thumb_height && $origw > $thumb_width)
{
$thumbnail_width = $image_width * $width_ratio;								// shrink width 
$thumbnail_height = $image_height * $width_ratio;							// shrink height	
}	
}

$thumbnail = imagecreatetruecolor($thumbnail_width,$thumbnail_height);
imagecopyresized($thumbnail,$image,0,0,0,0,$thumbnail_width,$thumbnail_height,$image_width,$image_height);
imagejpeg($thumbnail,NULL,$img_quality);
imagedestroy($thumbnail);
}
?>

Link to comment
Share on other sites

I found the solution in the user notes on the phpmanual page for the imagejpeg() function.

 

//Call this in the page you want the thumbnail
<img src="thumb.php?src=<?php echo $picturefile; ?>" />

 


//Thumb.php
<?php

header("Content-type: image/jpeg");

//Create Thumbnail Image
$image = imagecreatefromjpeg("uploads/$_GET[src]");							// Uploaded file location
$image_width=imagesx($image);													// Get Photo width
$image_height=imagesy($image);													// Get Photo height

// Thumbnail Dimensions
$thumb_width = 385;																// Thumbnail Display Width
$thumb_height = 289;																// Thumbnail Display Height

// Thumbnail Image Quality
$img_quality = 75;																// Image quality for the web is 75 dpi

$height_ratio = round($thumb_height / $image_height, 2);						// Calculate height shrink ratio
$width_ratio = round($thumb_width / $image_width, 2) ;							// Calculate width shrink ratio


if ($image_height <= $thumb_height && $image_width <= $thumb_width) 		
{
$thumbnail_width = $image_width;												//Keep original width		
$thumbnail_height = $image_height;												//Keep original height
}
else if ($image_height > $thumb_height || $image_width > $thumb_width)
{

if ( $image_height > $thumb_height && $image_width < $thumb_width )		
{

$thumbnail_width = $image_width * $height_ratio;							// shrink width 
$thumbnail_height = $image_height * $height_ratio;							// shrink height
}
else if ($image_width > $thumb_width && $image_height < $thumb_height )
{
//echo "here";
$thumbnail_width = $image_width * $width_ratio;								// shrink width 
$thumbnail_height = $image_height * $width_ratio;							// shrink height
}
else //($origh > $thumb_height && $origw > $thumb_width)
{
$thumbnail_width = $image_width * $width_ratio;								// shrink width 
$thumbnail_height = $image_height * $width_ratio;							// shrink height	
}	
}

$thumbnail = imagecreatetruecolor($thumbnail_width,$thumbnail_height);
imagecopyresized($thumbnail,$image,0,0,0,0,$thumbnail_width,$thumbnail_height,$image_width,$image_height);
imagejpeg($thumbnail,NULL,$img_quality);
imagedestroy($thumbnail);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.