Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

creating clearer unstretched thumbnails with gd and php

Posted on 2004-10-22
8
Medium Priority
?
292 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:paulp75
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 12

Expert Comment

by:minichicken
ID: 12387534
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
 
LVL 12

Expert Comment

by:minichicken
ID: 12387551
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
 
LVL 6

Author Comment

by:paulp75
ID: 12397523
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:minichicken
ID: 12398373
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
 
LVL 12

Expert Comment

by:minichicken
ID: 12398388
Just change:

$dest_width = 800;
$dest_height = 800;

to:

$dest_width = 120;
$dest_height = 120;
0
 
LVL 6

Author Comment

by:paulp75
ID: 12398787
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
 
LVL 12

Accepted Solution

by:
minichicken earned 1600 total points
ID: 12399164
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
 
LVL 6

Author Comment

by:paulp75
ID: 12399385
brilliant, exactly how i wanted it,
thank you so much.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question