Jump to content

Odd behavior: filenames (PHP or GD?)


sn33kyp3t3

Recommended Posts

Hello all. I am at a complete loss on this one. Hope somebody here has come across this before.

Here is the situation:

 

I am trying to generate some thumbnail images using gd.

If I pass my function (see below) a string literal, then everything works fine.

If I pass in a variable, then I get a black square for a thumb nail.

 

I've printed out the contents of the variable (image path) and used that as the literal, so I know the variable is good.

 

???

 

I have called the function like this:

createthumb('/absolute/path/to/img.jpg', '/home/for/thumbs/th_img.jpg', 100, 100); <------ Works perfectly

createthumb($imgName, '/home/for/thumbs/th_img.jpg', 100, 100); <------ Doesn't work

 


function createthumb($name, $filename, $new_h, $new_w){
        // find the file extension
    	$system=explode('.',$name);
    	
    	if (preg_match('/jpg|jpeg/',$system[1])){
    		$src_img=imagecreatefromjpeg($name);
    	}
    	if (preg_match('/png/',$system[1])){
    		$src_img=imagecreatefrompng($name);
    	}

        // get the original's dimensions
        $old_x=imageSX($src_img);
        $old_y=imageSY($src_img);

      
        
        // make the thumb proportional
        if ($old_x > $old_y) {
        	$thumb_w=$new_w;
        	$thumb_h=$old_y*($new_h/$old_x);
        }
        if ($old_x < $old_y) {
        	$thumb_w=$old_x*($new_w/$old_y);
        	$thumb_h=$new_h;
        }
        if ($old_x == $old_y) {
        	$thumb_w=$new_w;
        	$thumb_h=$new_h;
        }
       
        
        // create the image object, and copy the original
        $dst_img = @imagecreatetruecolor($thumb_w, $thumb_h);
    	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

        // save
        if (preg_match("/png/",$system[1]))
        {
        	imagepng($dst_img,$filename);
        } else {
        	imagejpeg($dst_img,$filename);
        }
        
       // clean up
        imagedestroy($dst_img);
        imagedestroy($src_img);
    }

Link to comment
https://forums.phpfreaks.com/topic/42423-odd-behavior-filenames-php-or-gd/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.