Jump to content

[SOLVED] resize a thumbnail from a panorama


brooksh

Recommended Posts

I want a thumbnail that is 100 wide by 75 high. My original image is 3000x400. So if I just change the width to 100, I have a thumbnail 100x10. If I change both the width and height to 100x75 then I get a messed up skewed image. Is it possible to set the height to 75, then cut off the rest of the width to 100? Here is what I have, but it doesn't work.

 

$oldimagesize = getimagesize($uploadFile);
$width = 100;
$height = 75;

$thumbsize = 75;
$width_orig = $oldimagesize[0];
$height_orig = $oldimagesize[1];

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}
	$thumbimagename = 'thumb_' . $newimagename;
	$thumbimagefull = $uploadDir . $thumbimagename;
	$thumbimage = imagecreatetruecolor($thumbsize, $thumbsize);
imagecopyresampled($thumbimage, $oldimage, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($thumbimage, null, 100);

Link to comment
Share on other sites

this should work  where $x and $y are the originals and obviously $newx and $newy is the new dimensions.  This resizes without losing the image ratio.

$ratio1 = $x/$newX;
    $ratio2 = $y/$newY;
    if($ratio1 > $ratio2)
    {
        $nx = $newX;
        $ny = $y / $ratio1;
    } else
    {
        $nx = $x / $ratio2;
        $ny = $newY;
    }

Link to comment
Share on other sites

I almost have it, but it is still skewed.

Something like this thumb996.jpg

to become this widecropped.jpg

$oldimagesize = getimagesize($uploadFile);
	$new_width = 100;
	$new_height = "75";
	$old_width = $oldimagesize[0];
	$old_height = $oldimagesize[1];


if($old_width > $old_height){  

    $biggestSide = $old_height;   
    $cropPercent = .5;   
    $cropWidth   = $old_width*$cropPercent;   
    $cropHeight  = $biggestSide*$cropPercent;   
    $c1 = array("x"=>($old_width-$cropWidth)/2, "y"=>($old_height-$cropHeight)/2);  
}else{  
    $biggestSide = $old_width;   
    $cropPercent = .5;   
    $cropWidth   = $biggestSide*$cropPercent;   
    $cropHeight  = old_height*$cropPercent;   
    $c1 = array("x"=>($old_width-$cropWidth)/2, "y"=>($old_height-$cropHeight)/2); 
}   


	$thumbimagename = 'thumb_' . $newimagename;
	$thumbimagefull = $uploadDir . $thumbimagename;

	$thumbimage = imagecreatetruecolor($new_width, $new_height);
	imagecopyresampled($thumbimage, $oldimage, 0, 0, $c1['x'], $c1['y'], $new_width, $new_height, $cropWidth, $cropHeight);

Link to comment
Share on other sites

I think this is what you want. May not be the most compact/efficient code but I was only interested in getting this to work. It will reduce the image until one axis is exactly the same size as one of the target dimensions and the other dimension is bigger than the target. It will then crop the other dimension to fit the target.

 

<?php

function fitImageToSize($filename, $width_new, $height_new)
{
    $src_x  = 0;
    $src_y  = 0;

    //Get dimensions of original image
    list($width_old, $height_old) = getimagesize($filename);

    //Resize & crop image based on smallest ratio
    if ( ($width_old/$height_old) < ($width_new/$height_new) )
    {
        //Determine Resize ratio on width
        $ratio = $width_new / $width_old;
        //Detemine cropping dimensions for height
        $crop =  $height_old - ($height_new/$ratio) ;
        $height_old = $height_old - $crop;
        $src_y = floor($crop/2);
    }
    else
    {
        //Detemine Resize ration on height
        $ratio = $height_new / $height_old;
        //Detemine cropping dimensions for width
        $crop = $width_old - ($width_new/$ratio);
        $width_old = $width_old - $crop;
        $src_x = floor($crop/2);
    }

    $image_old = imagecreatefromjpeg($filename);
    $image_new = imagecreatetruecolor($width_new, $height_new);
    imagecopyresampled($image_new, $image_old, 0, 0, $src_x, $src_y, $width_new, $height_new, $width_old, $height_old);
    return $image_new;
}


$new_image = fitImageToSize($uploadFile, 100, 75);

// Output
header('Content-type: image/jpeg');
imagejpeg($new_image, null, 100);

?>

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.