Page 1 of 2

GD image resize gives black result??!!

Posted: Thu May 13, 2004 10:49 am
by potato
Hi,

I have following script to resize a picture. Can someone tell me what i am doing wrong?
I'm almost killing myself!! aaarghhh :evil:
As result i get a black box with the size of the max width & height in the script.

Code: Select all

<?php
# Constants
define(IMAGE_BASE, '/artists/Piddle/hoesthumb.jpg');
define(MAX_WIDTH, 300);
define(MAX_HEIGHT, 150);

# Get image location
$image_file = str_replace('..', '', $_SERVER['QUERY_STRING']);
$image_path = IMAGE_BASE . "/$image_file";

# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
    $img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
    $img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
    $img = @imagecreatefrompng($image_path);
}

# If an image was successfully loaded, test the image for size
if ($img) {

    # Get image size and scale ratio
    $width = imagesx($img);
    $height = imagesy($img);
    $scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

    # If the image is larger than the max shrink it
    if ($scale < 1) {
        $new_width = floor($scale*$width);
        $new_height = floor($scale*$height);

        # Create a new temporary image
        $tmp_img = imagecreatetruecolor($new_width, $new_height);

        # Copy and resize old image into new image
        imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
                         $new_width, $new_height, $width, $height);
        imagedestroy($img);
        $img = $tmp_img;
    }
}

# Create error image if necessary
if (!$img) {
    $img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
    imagecolorallocate($img,0,0,0);
    $c = imagecolorallocate($img,70,70,70);
    imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
    imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}

# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>

Posted: Thu May 13, 2004 10:58 am
by xisle
there looks like a couple of blank lines at the top of the script, that could break the header()

Posted: Thu May 13, 2004 11:04 am
by xisle
you could also leave the image on the server and call it through html

Code: Select all

imagejpeg ($img,"pic.jpg"); 
imagedestroy ($img); 
?>
<center><img src="pic.jpg" border=0></center>

Posted: Thu May 13, 2004 11:07 am
by potato
What do you mean with blank lines?

Posted: Thu May 13, 2004 11:20 am
by launchcode
He meant before the opening <?php tag there are blank lines - which will kill the Header() function at the bottom of the script.

However I don't think that is the cause of your problem otherwise you'd get a header error, not just a black image. Take out the @ signs before the createimage functions so you can see if they give you an error or not.

Posted: Thu May 13, 2004 11:35 am
by potato
I deleted the @ signs, but still the black box.

Posted: Thu May 13, 2004 11:39 am
by markl999
define(IMAGE_BASE, '/artists/Piddle/hoesthumb.jpg');
define(MAX_WIDTH, 300);
define(MAX_HEIGHT, 150);
Those are all incorrect. It should be:

Code: Select all

define('IMAGE_BASE', '/artists/Piddle/hoesthumb.jpg');
define('MAX_WIDTH', 300);
define('MAX_HEIGHT', 15);
Without the single quotes it will cause an undefined constant error and in turn cause a 'cannot be displayed, because it contains errors'

Posted: Thu May 13, 2004 11:45 am
by potato
No, nothing. But could it be that it's the image_base? I'm not sure what to write here.

The script is in the folder artists, and the folder artists is in the roots folder. Or do i have to give the full directory or how you call that?

Posted: Thu May 13, 2004 11:47 am
by markl999
Sorry, yes, your IMAGE_BASE was wong too. Your code worked fine for me if i did $image_path = '/hard/coded/path/to/foo.jpg';

So your IMAGE_BASE needs to be :
define('IMAGE_BASE', '/full/path/to/the/artists/Piddle/');

as you later append the image name onto the end of that.

Posted: Thu May 13, 2004 11:48 am
by potato
Or maybe bether, does someone knows a good resize script? Where i only have to pput a img tag to a php script or something?
I already have searched on many sites but without success.

Posted: Thu May 13, 2004 11:49 am
by potato
aaaaa, and how to find the full path? is there any trick? Or do i have to ask at my administrator?

Posted: Thu May 13, 2004 11:51 am
by markl999
Your resize code is ok, it works for me, you just have some initial constnat/path problems, but the actual resizing works ok.
But, rather than resize every image, every time, it might be better to save the resized image to a file and then you don't need to go through the expense of resizing every image, every time. Just check if the resized image exists and if so, you don't need to resize the original again.

Posted: Thu May 13, 2004 11:52 am
by markl999
aaaaa, and how to find the full path?
Presuming /artists/Piddle is off your document root, you could do:
define('IMAGE_BASE', $_SERVER['DOCUMENT_ROOT'].'/artists/Piddle');

Posted: Thu May 13, 2004 12:16 pm
by potato
I noh have this:

define('IMAGE_BASE', '/home/httpd/vhosts/bevibed.be/httpdocs/artists/Piddle/hoesthumb.jpg');

define('MAX_WIDTH', 300);
define('MAX_HEIGHT', 150);


but still the black box.
A friend told me something about true colors, could that it be?

:oops: Sorry for all the trouble :oops:

Posted: Thu May 13, 2004 12:19 pm
by markl999
But in your code you do:
$image_path = IMAGE_BASE . "/$image_file";

Yet IMAGE_BASE is not a path, it's an image. So you'de end up with :
/home/httpd/vhosts/bevibed.be/httpdocs/artists/Piddle/hoesthumb.jpg/someimage.jpg

IMAGE_BASE needs to be a path, not a path/image, so try:

define('IMAGE_BASE', '/home/httpd/vhosts/bevibed.be/httpdocs/artists/Piddle/');

Then if image_file is 'foo.jpg' then image_path becomes:
/home/httpd/vhosts/bevibed.be/httpdocs/artists/Piddle/foo.jpg