Jump to content

Thumbnail stretches :\


kevin_newbie

Recommended Posts

Hello,

 

I have a thumbnail script that is stretching the image. I do not want that, I want to be able to set the width and height of the image, then it crops what ever is left. I left an attachment of what the images are doing so you know what I am talking about.

 

This is what I have now;

header("Content-Type: image/jpeg");
$source = $_GET['pic'];

$size = getimagesize($source);
          
$setWidth = 238;
$setHeight = 143;

$width = $size[0];
$height = $size[1];
          
if($width> $height) 
{
$x = ceil(($width - $height) / 2 );
$width = $height;
}

else if($height> $width) 
{
$y = ceil(($height - $width) / 2);
$height = $width;
}

$new_im = ImageCreatetruecolor($setWidth,$setHeight);
$im = imagecreatefromjpeg($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$setWidth,$setHeight,$width,$height);
imagejpeg($new_im,$dest,100);

 

Then for my php page I am doing:

<img src="filename.php?pic=images/1.jpg\" />

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

this is somewhat what your looking for.

But this should give you the idea..

 



<?php
/*
* This script demonstrates the mathematics of resizing an image to a different
* width and height without stretching or squeezing the original image. If the
* source image has a different aspect ratio than the destination image, the
* source image will be cropped.
* 
* Consider a source image with a width of 320 pixels and a height of 240
* pixels. The destination image is to be 80 pixels by 80 pixels. If these
* dimensions were passed directly to imagecopyresampled(), the resized image
* would appear to have a squeezed width. To prevent this, the width of the
* source image should be cropped to 240 pixels so that the ratio of its width
* to its height is 1:1, which is the same as the destination image's ratio.
* When the source image is resized, it will not appear squeezed.
*/

$src = imagecreatefromjpeg('sample.jpg'); // Source image

$dw = 80;                    // Destination image Width
$dh = 80;                    // Destination image Height

$sw = imagesx($src);         // Source image Width
$sh = imagesy($src);         // Source image Height

$wr = $sw / $dw;             // Width Ratio (source:destination)
$hr = $sh / $dh;             // Height Ratio (source:destination)

$cx = 0;                     // Crop X (source offset left)
$cy = 0;                     // Crop Y (source offset top)

if ($hr < $wr)               // Height is the limiting dimension; adjust Width
{
    $ow = $sw;               // Old Source Width (temp)
    $sw = $dw * $hr;         // New virtual Source Width
    $cx = ($ow - $sw) / 2;   // Crops source width; focus remains centered
}
if ($wr < $hr)               // Width is the limiting dimension; adjust Height
{
    $oh = $sh;               // Old Source Height (temp)
    $sh = $dh * $wr;         // New virtual Source Height
    $cy = ($oh - $sh) / 2;   // Crops source height; focus remains centered
}
// If the width ratio equals the height ratio, the dimensions stay the same.

$dst = imagecreatetruecolor($dw, $dh); // Destination image
imagecopyresampled($dst, $src, 0, 0, $cx, $cy, $dw, $dh, $sw, $sh);

// Previews the resized image (not saved)
header('Content-type: image/jpeg');
imagejpeg($dst);
?>

 

Hopefully this Helps :)

Link to comment
Share on other sites

Crayon Violent,

 

Thanks for the link and let me check it out. :)

 

Hi ProXy_

 

I tried the script you sent me and what it does is crops the picture but then distorts it, I don't want that. I like what I have now I just don't like the whole stretching out the image, as you can see in the attached on my first post.

 

Thanks though.

 

 

Link to comment
Share on other sites

I use something similar to the code below. It won't crop the image, or make it an exact size - it will resize to either the max-width or max-height depending on whether the image is landscape or portrait.

 

Hope it an be of some use...

 

  define('MAX_WIDTH', 500);
  define('MAX_HEIGHT', 500);
  
  // process the uploaded image
    $original = ($_FILES['filelogo']['tmp_name']);
    // begin by getting the details of the original
    list($width, $height, $type) = getimagesize($original);
// calculate the scaling ratio
    if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
      $ratio = 1;
      }
    elseif ($width > $height) {
      $ratio = MAX_WIDTH/$width;
      }
    else {
      $ratio = MAX_HEIGHT/$height;
      }

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.