GD image resize gives black result??!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

GD image resize gives black result??!!

Post 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);
?>
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

there looks like a couple of blank lines at the top of the script, that could break the header()
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post 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>
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

What do you mean with blank lines?
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

I deleted the @ signs, but still the black box.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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'
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post 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.
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

aaaaa, and how to find the full path? is there any trick? Or do i have to ask at my administrator?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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');
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post 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:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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
Post Reply