Solved

creating clearer unstretched thumbnails with gd and php

Posted on 2004-10-22
270 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
Question by:paulp75
    8 Comments
     
    LVL 12

    Expert Comment

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

    Expert Comment

    by:minichicken
    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
    Just change:

    $dest_width = 800;
    $dest_height = 800;

    to:

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

    Author Comment

    by:paulp75
    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:
    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
    brilliant, exactly how i wanted it,
    thank you so much.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    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…
    Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now