Help - Search - Members - Calendar
Full Version: PHP - Image Resizing Problem
Invision Power Services > Community Forums > Community Web Design and Coding
Michael Boutros
Hello everyone. I am writing a script to resize images, and am stuck with this problem. I know for a fact that the problem is NOT that any of the variables are unset, because I have checked and they are all set. If anyone could help I would be very appreciative original.gif

CODE
// Make new image
$new = imagecreatetruecolor($new_width, $new_height);

// Create source image
if ($file_type_ns == "jpg" OR $file_type_ns == "jpeg"){
   $source = imagecreatefromjpeg("uploads/$file_name");
}
    if ($file_type_ns == "gif"){
   $source = imagecreatefromgif("uploads/$file_name");
}
    if ($file_type_ns == "png"){
   $source = imagecreatefrompng("uploads/$file_name");
}

// Build new image
imagecopyresampled($new, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output new image
if ($info_type == "jpeg" OR $info_type == "jpg"){
  imagejpeg($new, "images/$file_name_ns.jpeg", $quality);
}

if ($info_type == "gif"){
  imagegif($new, "images/$file_name_ns.gif");
}

if ($info_type == "png"){
  imagepng($new, "images/$file_name_ns.png");
}


$file_type_ns: the file type from $_FILES with the suffix extension
$info_type: the new file type that a user has already chosen from a form

Once again, all these variables have already been set. I get no errors or anything, but when I go to look at the new image, it is the right size and type, but it is completely black. Thanks to anyone who can help original.gif
Michael Boutros
Anyone? I really, really need this please!
Brendon Koz
I read your comment without looking at the actual code.

By chance, was your test image a GIF file with transparency (or without)?

GIF files, supposedly, don't resize well using the GD functions. For instance, either Plogger or Gallery (image gallery script) will not resize GIF images properly, instead you get a properly sized BLACK filled image.

Obviously, since one of the two handles it properly, there's a workaround. I, however, am not sure what this is. Try a PNG and JPG file as a test and see if your function works well for those image types; then research the GIF thing by itself.
bılʞ
I'd actually like an answer to this as well.
Michael Boutros
I made a stupid mistake elsewhere in the code, I misnamed a variable that got the old file dimensions blushing.gif Thanks for your help guys original.gif
SMGENca
You're going to get black on any resized transparent gif (or possibly gray on 8 bit png) using that code because you're using imagecreatetruecolor which creates the canvas with a black pallette and you're not handling the transparency at all.

you need to do something like
CODE
$new = imagecreatetruecolor($new_width, $new_height);
$transparent = imagecolorallocate($new, 200, 255, 200);
  imagefill($new, 0, 0, $transparent);
  imagecolortransparent($new, $transparent);


You might be able to read the transparent color from the original gif to reduce the number of colors
ie:
CODE
$new = imagecreatetruecolor($new_width, $new_height);
$transparent = imagecolorat($source, 0,0);
  imagefill($new, 0, 0, $transparent);
  imagecolortransparent($new, $transparent);

but it doesn't work in every case and you'd want to apply it only in the case of gif or some png images.


Also, are you destroying all image resources when you finish?
I don't see it in your code but maybe you truncated that for the example.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.