creating clearer unstretched thumbnails with gd and php

what is happening when i upload a photo, my script creates a thumbnail at the size of 120x120. which is the perfect size.
only problem is that when it is a long photo or a wide photo it will just shrink the picture and make the thumbnail quite awful looking as the ratio is distorted.
is there a way to just get the center of a picture instead of using the whole photo to base the thumbnail creation upon.

here is the current code.
thanks


if($_POST['submit']) //If submit is hit
{
$dirpics = "memberspics";
$dirthumbs = "membersthumbs";
$destpic = $dirpics."/".$image_name;

$src_image = imagecreatefromjpeg($_FILES['image']['tmp_name']); //load the uploaded image into memory for manipulation

$src_width = imagesx($src_image); //original width
$src_height = imagesy($src_image); //original height
$src_ratio = $src_height / $src_width; //width and height ratio to preserve aspect ratio for thumbnail

$thumb_width = 120; //fixed the thumbnail width
$thumb_height = 120; //get thumbnail height according to thumbnail width * the ratio

$thumb_image = imagecreatetruecolor($thumb_width, $thumb_height); //create blank image for thumbnail
imagecopyresized($thumb_image, $src_image, 0,0,0,0, $thumb_width, $thumb_height, $src_width, $src_height); //create thumbnail from source image

imagejpeg($thumb_image, "membersthumbs/thumbnail.jpg"); //save thumbnail as

LVL 6
paulp75Asked:
Who is Participating?
 
minichickenConnect With a Mentor Commented:
This should be what you looking for...

<?
$src_image = imagecreatefromjpeg("path/square.jpg"); //load the uploaded image into memory for manipulation

$src_width = imagesx($src_image); //original width
$src_height = imagesy($src_image); //original height

if ($src_width > $src_height) //if width is longer
{
      $diff = $src_width - $src_height;
      $sqr_image = imagecreatetruecolor($src_height, $src_height); //create a square image
      $sqr_x = $diff/2;
      $sqr_y = 0;
      imagecopyresized($sqr_image, $src_image, 0,0, $sqr_x, $sqr_y, $src_width, $src_height, $src_width, $src_height);
}

if ($src_width < $src_height) //if height is longer
{
      $diff = $src_height - $src_width;
      $sqr_image = imagecreatetruecolor($src_width, $src_width); //create a square image
      $sqr_x = 0;
      $sqr_y = $diff/2;
      imagecopyresized($sqr_image, $src_image, 0,0, $sqr_x, $sqr_y, $src_width, $src_height, $src_width, $src_height);
}

if ($src_width == $src_height) //if height = width
{
      $sqr_image = imagecreatetruecolor($src_width, $src_width); //create a square image
      imagecopyresized($sqr_image, $src_image, 0, 0, 0, 0, $src_width, $src_height, $src_width, $src_height);
}

$sqr_width = imagesx($sqr_image);
$sqr_height = imagesy($sqr_image);

$thumb_width = 120;
$thumb_height = 120;

$thumb_image = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresized($thumb_image, $sqr_image, 0, 0, 0, 0, $thumb_width, $thumb_height, $sqr_width, $sqr_height);
imagejpeg($thumb_image); //save thumbnail as
?>
0
 
minichickenCommented:
Calculation for getting centeral part of image:
http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Databases/Q_21169774.html

This should help...
0
 
minichickenCommented:
According to http://Q_21169774.html above, the code should look like this:


$src_image = imagecreatefromjpeg($_FILES['image']['tmp_name']); //load the uploaded image into memory for manipulation

$src_width = imagesx($src_image); //original width
$src_height = imagesy($src_image); //original height

$dest_width = 120;
$dest_height = 120;

$thumb_width = ($src_width - $dest_width) / 2;
$thumb_height = ($src_height - $dest_height) / 2;

$thumb_image = imagecreatetruecolor($thumb_width, $thumb_height); //create blank image for thumbnail
imagecopyresized($thumb_image, $src_image, 0,0,0,0, $thumb_width, $thumb_height, $src_width, $src_height); //create thumbnail from source image

imagejpeg($thumb_image, "membersthumbs/thumbnail.jpg"); //save thumbnail as
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
paulp75Author Commented:
seems to be just outputting the top left hand side of the image in a smaller version, not to the 120x120 dimensions which i wanted.
so it just basically output the background of the original image (719w x 959h) and output it as (299wx419h)
so the face of the person can't be seen at all.
thanks for your attempt though.
0
 
minichickenCommented:
This should be alright....

<?
$src_image = imagecreatefromjpeg("path/the_pic.jpg"); //load the uploaded image into memory for manipulation

$src_width = imagesx($src_image); //original width
$src_height = imagesy($src_image); //original height

$dest_width = 800;
$dest_height = 800;

$thumb_x = ($src_width/2) - ($dest_width/2);
$thumb_y = ($src_height/2) - ($dest_height/2);

$thumb_image = imagecreatetruecolor($dest_width, $dest_height); //create blank image for thumbnail
imagecopyresized($thumb_image, $src_image, 0,0, $thumb_x, $thumb_y, $src_width, $src_height, $src_width, $src_height); //create thumbnail from source image

imagejpeg($thumb_image); //save thumbnail as
?>
0
 
minichickenCommented:
Just change:

$dest_width = 800;
$dest_height = 800;

to:

$dest_width = 120;
$dest_height = 120;
0
 
paulp75Author Commented:
ok now whats happening is that it is only taking, what seems to be the most central 120x120, which in the case of the pic that i am trying to upload is only producing the teeth. all that i want to do is just chop the long ends off the pic to make it square, and then reduce it in size to 120x120.


i thought i was on to something, when i came up with this

$src_image = imagecreatefromjpeg($_FILES['image']['tmp_name']); //load the uploaded image into memory for manipulation

$src_width = imagesx($src_image); //original width
$src_height = imagesy($src_image); //original height
if($src_width < $src_height)
{
$tempsize = $src_height-$src_width;
$thumb_x = $tempsize;
$thumb_y = 0;
$temp_width = $src_width;
$temp_height = $src_width;
}
else
{
$tempsize = $src_width-$src_height;
$thumb_x = 0;
$thumb_y = $tempsize;
$temp_width = $src_height;
$temp_height = $src_height;

}


$dest_width = 120;
$dest_height = 120;


$thumb_image = imagecreatetruecolor($dest_width, $dest_height); //create blank image for thumbnail
imagecopyresized($thumb_image, $src_image, 0,0,0,0, $src_width, $src_height, $temp_width, $emp_height); //create thumbnail from source image

imagejpeg($thumb_image, "membersthumbs/thumbnail.jpg"); //save thumbnail


not sure if i'm coming close to that, by trying to get it to start at coordinate $tempsize.
so if the image was say 800 height by 600 width.
then it would calculate 800-600 which would equal 200.
then it would start at 200 pixels down, and 0 across
and if it was 600 height by 800 width
it would start at 0 pixels down, and 200 across.
how do i do that.

sorry if i didnt explain that enough at first.
your help is very much appreciated. giving me a lot of direction,
trying my best to learn it.
:)
thanks
0
 
paulp75Author Commented:
brilliant, exactly how i wanted it,
thank you so much.
0
All Courses

From novice to tech pro — start learning today.