Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    php image upload broken

    Hi

    I have inherited a site that creates a database of profiles. Part of this process is to upload an image, and re-size it to three different sizes.

    Its worked fine for about 5 or 6 years when recently it suddenly started to produce 3 different sized black images.

    I have tracked this down to a function, but I can't seem to find out what's wrong.

    here are the errors that I have echoed out
    Code:
    $target_type=image/jpeg
    imagecreatetruecolor was true
    
    Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /websites/LinuxPackage02/st/om/pm/..../public_html/admin/php/nslib_php/nslib_image.php on line 287
    
    Warning: imagedestroy(): supplied argument is not a valid Image resource in /websites/LinuxPackage02/st/om/pm/.../public_html/admin/php/nslib_php/nslib_image.php on line 293
    I'd really apprecaite any help anyone might be able to offer.

    Here is the funtion - apologies for posting the whole thing - hope its not too long

    Many thanks

    Code:
    function resize_image ($source_image, $target_image, $target_width, $target_height, $crop_position = false, $quality = 75) {
    
    	// set the memory limit higher and the execution time higher for the script as large
    
    	// attachments may be processed
    
    	ini_set('memory_limit', '32M');
    
    	ini_set('max_execution_time', '60');
    
    	$return_var = false;		// initialise the return variable
    
    	
    
    
    
    	// get the source file type
    
    	$source_type = get_mime_type($source_image);
    
    	
    
    	// get the target file type
    
    	if (is_numeric(strpos(strtolower($target_image), 'jpg')) || is_numeric(strpos(strtolower($target_image), 'jpeg'))) {
    
    		// if the type is jpeg
    
    		$target_type = 'image/jpeg';
    
    	} elseif (is_numeric(strpos(strtolower($target_image), 'gif'))) {
    
    		// if the type id gif
    
    		$target_type = 'image/gif';
    
    	}
    
    		
    
    	// get the dimensions of the source image and check whether the file is valid
    
    	if (list($source_width, $source_height) = getimagesize($source_image)) {
    
    
    
    		// calculate the dimensions of the new photo
    
    		// if there is no width set then calculate from the ratio of the source image and
    
    		// the height dimension
    
    		if (!$target_width) {
    
    			$target_width = ($source_width / $source_height ) * $target_height;
    
    		}
    
    		// if there is no height set then calculate from the ratio of the source image and
    
    		// the width dimension
    
    		if (!$target_height) {
    
    			$target_height = ($source_height / $source_width ) * $target_width;
    
    		}
    
    	
    
    		// if the target filetype is a jpeg, create a truecolour image
    
    		if ($target_type == 'image/jpeg') {
    			//edward
    			echo '<h2>$target_type='.$target_type.'</h2>';
    			// create an empty image in memory with the target's dimensions
    			
    			//$target_file = imagecreatetruecolor($target_width, $target_height);
    
    			if($target_file = imagecreatetruecolor($target_width, $target_height)){
    				
    				echo '<h2>imagecreatetruecolor was true</h2>';
    				}else{
    				echo '<h2>imagecreatetruecolor was false</h2>';
    				};
    			
    			
    			
    		} else {
    
    			// otherwise create a pallete based image
    
    			// create an empty image in memory with the target's dimensions
    
    			$target_file = imagecreate($target_width, $target_height);
    
    		}
    //added this
    //$target_image = imagecreatefromjpeg($target_file);
    //$source_image = imagecreatefromjpeg($source_file);
    //end of added this
    
    		// if the source image is a jpeg then load it
    
    		if ($source_type == 'image/jpeg') {
    
    	echo '<h2>source_type='.$source_type.'</h2>';		
    	// load the source image as a jpeg from disk into memory
    
    
    
    
    $source_file = imagecreatefromjpeg($source_image);
    
    /* edward if($source_file = imagecreatefromjpeg($source_image)){
    				echo '<h2>imagecreatefromjpeg was true</h2>';
    			}else{
    				echo '<h2>imagecreatefromjpeg was false</h2>';
    			};*/
    
    		} elseif ($source_type == 'image/gif') {
    
    			// otherwise load the source image as a gif from disk into memory
    
    			$source_file = imagecreatefromgif($source_image);
    
    		}
    
    	  
    
    		// preset the source x and y positions of the output image
    
    	  	$source_x = 0;
    
    		$source_y = 0;
    
    		
    
    		// copy the source width and height to some other variables used in calculation
    
    		$calc_width = $source_width;
    
    		$calc_height = $source_height;
    
    	  
    
    		// if it's specified that the image should be cropped
    
    		if ($crop_position) {
    
    		
    
    			// calculate the source & target ratios
    
    			$source_ratio = $source_width / $source_height;
    
    			$target_ratio = $target_width / $target_height;
    
    			
    
    			// calculate the other dimension
    
    			if ($source_ratio > $target_ratio) {
    
    				$source_width = ($target_width / $target_height ) * $calc_height;
    
    			} else {
    
    				$source_height = ($target_height / $target_width ) * $calc_width;
    
    			}
    
    			
    
    			// depending on which crop position is selected, the calculations will be
    
    			// done to extract the required part
    
    			switch ($crop_position) {
    
    				case 'top':
    
    					$source_x = ($calc_width - $source_width) / 2;
    
    					$source_y = 0;
    
    					break;
    
    				case 'bottom':
    
    					$source_x = ($calc_width - $source_width) / 2;
    
    					$source_y = $calc_height - $source_height;
    
    					break;
    
    				case 'left':
    
    					$source_x = 0;
    
    					$source_y = ($calc_height - $source_height) / 2;
    
    					break;
    
    				case 'right':
    
    					$source_x = $calc_width - $source_width;
    
    					$source_y = ($calc_height - $source_height) / 2;
    
    					break;
    
    				default:
    
    					$source_x = ($calc_width - $source_width) / 2;
    
    					$source_y = ($calc_height - $source_height) / 2;
    
    					break;
    
    			}
    
    		}
    
    		
    
    		// if a jpeg is to be created
    
    		if ($target_type == 'image/jpeg') {
    
    			// RESIZE the image with the parameters specified
    
    			imagecopyresampled($target_file, $source_file, 0, 0, $source_x, $source_y, $target_width, $target_height, $source_width, $source_height);
    
    			
    
    			// remove the image of the source file from memory
    
    			imagedestroy($source_file);
    
    			
    
    			// create a jpeg file on disk from the newly created image in memory and write to disk
    
    			$return_var = imagejpeg($target_file,$target_image, $quality);
    
    			// remove the image of the target file from memory
    
    			imagedestroy($target_file);
    
    			
    
    		// if a gif file is to be created
    
    		} elseif ($target_type == 'image/gif') {
    
    			// RESAMPLE the image with the parameters specified
    
    			imagecopyresized($target_file, $source_file, 0, 0, $source_x, $source_y, $target_width, $target_height, $source_width, $source_height);
    
    			
    
    			// remove the image of the source file from memory
    
    			imagedestroy($source_file);
    
    			
    
    			// create a gif file on disk from the newly created image in memory and write to disk
    
    			$return_var = imagegif($target_file,$target_image);
    
    			// remove the image of the target file from memory
    
    			imagedestroy($target_file);
    
    		}
    
    	}
    
    	return $return_var;		// return the status of the function
    
    }

  • #2
    Regular Coder
    Join Date
    Sep 2012
    Posts
    100
    Thanks
    5
    Thanked 3 Times in 3 Posts
    This may or may not be the case with your site. but its been my experience that due to software upgrades, code written years ago does not always work well or at all..

  • #3
    New Coder
    Join Date
    Aug 2006
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, I'd have said that too. I have contacted the host, raised a ticket etc etc and they tell me there have been no upgrades...

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Black images eh? Before I look at the code, can you determine which file type(s) are doing that? PNG in particular seems to have an issue with this as it has an alpha channel that the JPEG doesn't have. Test it out with a JPG and a PNG file to see if both are black or if one is black.
    Those the only two errors you're seeing from this output as well? I don't see an error on failing to create, so this leads me to believe (before I go through the code) that there is a branch that your not getting to that you expect to get to.

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi - thanks for the reply

    There is also this :
    Code:
    Array
    (
        [name] => 14032010216.jpg
        [type] => image/pjpeg
        [tmp_name] => /tmp/phpbvTSes
        [error] => 0
        [size] => 65469
    )
    which I think sugests that the upload has happened ok.

    The errors above print out 3 times, because of the loop for the 3 resizes

    All the above was for jpegs

    When I try png the array [name] says its a .png but the and the errors are
    Code:
    $target_type=image/jpeg
    imagecreatetruecolor was true
    ie it appears to test as a jpeg?

    png and jpeg both show black rectangles.

    Thanks for saying you'd look at this.

  • #6
    New Coder
    Join Date
    Apr 2010
    Posts
    55
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Just like any PHP error, you need to think logically and try to track the error. You can't assume things - bug fixing is a logical process.

    Here you are getting error "imagecopyresampled(): supplied argument is not a valid Image resource" so obviously the error comes from the source file supplied, which possibly do not exist / was not created. Source is named $source_file , so you need to go to the place where that variable was created. I see it, for jpeg images:
    $source_file = imagecreatefromjpeg($source_image);

    Check and echo both $source_file and $source_image after this line and see if there is any error. Go backwards, if needed untill you find the source of the problem. Once the source is identified, check the PHP configurations via php_info(), check the manual and the comments on the http://php.net etc.

    This is fairly a simple problem and only you can find the reason since you are the only one with access to the server. Don't panic, take it step by step going backwards.
    Hosting Reviews and Discounts: Bluehost Coupon and Hostmonster Coupon

  • #7
    New Coder
    Join Date
    Aug 2006
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks. WIll go and put in those echo codes and report back


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •