wtf? I cannot figure out imagecopyresampled

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

wtf? I cannot figure out imagecopyresampled

Post by Luke »

I have been working on figuring out exactly how imagecopyresampled works for the last like 4 hours. I cannot figure it out. What do all of the parameters do? I just CANT seem to get it to do anything I want. No matter what I try I end up with black space on one side of the image. WTF?

Code: Select all

<?php
 
$imagesrc_location = 'C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\castle.jpg';
$imagedst_location = 'C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\castle_resampled.jpg';
 
// Get new sizes
list($srcwidth, $srcheight) = getimagesize($imagesrc_location);
list($dstwidth, $dstheight) = array($srcwidth, ($srcheight));
 
$imagedst = imagecreatetruecolor($dstwidth, $dstheight); // I think some of my problems stem from here... I dont know if I'm creating the right dimensions for the file
$imagesrc = imagecreatefromjpeg($imagesrc_location);
 
if (imagecopyresized($imagedst, $imagesrc, 0, 0, 0, 0, $dstwidth, $dstheight, $srcwidth, $srcheight)) { // what do all those zeroes do? I tried figuring it out, but it just doesn't make sense to me
 
    // Output image
    header('Content-type: image/jpeg');
    imagejpeg($imagedst);
 
} else {
 
    echo "Could not resize file";
 
}
I feel like an idiot. Maybe it's just really late in the day and I need sleep. :(
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: wtf? I cannot figure out imagecopyresampled

Post by Inkyskin »

It's a lot more simple than it looks :) Those zero's are usually used when cropping and messing about with offsets etc.

This line confuses me:

Code: Select all

list($dstwidth, $dstheight) = array($srcwidth, ($srcheight));
I have just changed your code slightly so that when copying the image, you use the originals width and height divided by 2 - see if that brings a normal result (Without extra pap). I also tend to use imagecopyresampled instead as it usuallu gives a cleaner result.

Code: Select all

<?php
 
$imagesrc_location = 'C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\castle.jpg';
$imagedst_location = 'C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\castle_resampled.jpg';
 
// Get new sizes
list($srcwidth, $srcheight) = getimagesize($imagesrc_location);
 
$dstwidth = $srcwidth / 2;
$dstheight = $srcheight / 2;
 
$imagedst = imagecreatetruecolor($dstwidth, $dstheight);
$imagesrc = imagecreatefromjpeg($imagesrc_location);
 
if (imagecopyresampled($imagedst, $imagesrc, 0, 0, 0, 0, $dstwidth, $dstheight, $srcwidth, $srcheight)) {
 
    // Output image
    header('Content-type: image/jpeg');
    imagejpeg($imagedst);
 
} else {
 
    echo "Could not resize file";
 
}
From php.net for imagecopyresized
Parameters

dst_im
--Destination image link resource

src_im
--Source image link resource

dst_x
--x-coordinate of destination point

dst_y
--y-coordinate of destination point

src_x
--x-coordinate of source point

src_y
--y-coordinate of source point

dst_w
--Destination width

dst_h
--Destination height

src_w
--Source width

src_h
--Source height
From php.net for imagecreatetruecolor
Parameters

width
--Image width

height
--Image height
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: wtf? I cannot figure out imagecopyresampled

Post by Luke »

What I'm trying to do is crop the image. I can't figure out how to do that.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: wtf? I cannot figure out imagecopyresampled

Post by Inkyskin »

This is a tidied up version of a couple of functions I use. First function just makes the image smaller, the second one turns it into a sqaure:

You should be able to modify one of these to do what you need. Both take a max size, which for the first function sets the max width or height, depending on which is biggest. On the second, I work out the smallest dimension, then resize the image, cropping off the excess from the longest dimension.

Code: Select all

   function resize_normal($image_type,$file_old,$file_new,$max_size)
    {
        //
        // $image_type should be either:
        //
        // png, jpg or gif
        //
        list($width, $height) = getimagesize($file_old);
        if($width <= $max_size && $height <= $max_size){
            copy($file_old, $file_new);
        } else {
            if ($width > $height){
                $resize_percent = $max_size / $width;
                $new_height = $height * $resize_percent;
                $new_width = $max_size;
            } else {
                $resize_percent = $max_size / $height;
                $new_height = $max_size;
                $new_width = $width * $resize_percent;
            }
            $temp_image = imagecreatetruecolor($new_width, $new_height);
            switch($image_type){
                case 'png':
                    $image = imagecreatefrompng($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    imagepng($temp_image, $file_new);
                    break;
                case 'jpg':
                    $image = imagecreatefromjpeg($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    imagejpeg($temp_image, $file_new);
                    break;
                case 'gif':
                    $image = imagecreatefromgif($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    imagegif($temp_image, $file_new);
                    break;
            }
        }
    }
 
 
    function resize_square($image_type,$file_old,$file_new,$max_size)
    {
        //
        // $image_type should be either:
        //
        // png, jpg or gif
        //
        list($width, $height) = getimagesize($file_old);
        if($width <= $max_size && $height <= $max_size){
            copy($file_old, $file_new);
        } else {
            if ($width > $height) {
                $src_w = $src_h = $height;
                $src_y = 0;
                $src_x = round(($width - $height) / 2);
            } else {
                $src_w = $src_h = $width;
                $src_x = 0;
                $src_y = round(($height - $width) / 2);
            }
            $temp_image = imagecreatetruecolor($max_size, $max_size);
            switch($image_type){
                case 'png':
                    $image = imagecreatefrompng($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, $src_x, $src_y, $max_size, $max_size, $src_w, $src_h);
                    imagepng($temp_image, $file_new);
                    break;
                case 'jpg':
                    $image = imagecreatefromjpeg($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, $src_x, $src_y, $max_size, $max_size, $src_w, $src_h);
                    imagejpeg($temp_image, $file_new);
                    break;
                case 'gif':
                    $image = imagecreatefromgif($file_old);
                    imagecopyresampled($temp_image, $image, 0, 0, $src_x, $src_y, $max_size, $max_size, $src_w, $src_h);
                    imagegif($temp_image, $file_new);
                    break;
            }
        }
    }
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Re: wtf? I cannot figure out imagecopyresampled

Post by dbevfat »

The Ninja Space Goat wrote:What I'm trying to do is crop the image. I can't figure out how to do that.
If you use WideImage (see sig), that's how crop is done:

Code: Select all

wiImage::load('original.png')->crop(10, 10, 50, 50)->saveToFile('cropped.png');
Post Reply