• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 536
  • Last Modified:

php error with imagepng function

Hello,

I have a php script that is creating an image using the imagepng function using the below code and its throwing the below error.  Does anyone know how I can fix this or better troubleshoot this?  echo statements wont show up in my log files...but im new to php so I may have something configured wrong.

Statement causing error:
Error:
PHP Fatal error:  imagepng() [<a href='function.imagepng'>function.imagepng</a>]: gd-png:  fatal libpng error: zlib error in /var/www/html/swfupload/upload.php on line 153

<?php
error_reporting(E_ALL);
	#	SWFUpload Email Notify
		# SWFUpload upload.php with simple file saving and email reporting
		# by Eric Pecoraro ( eric dot pecoraro at shepard dot com )
		# ***USE AT YOUR OWN RISK***  ***NO WARRENTIES EXPRESSED OR IMPLIED***
		# THIS SCRIPT WAS *NOT* WRITTEN FOR SECURITY, BUT RATHER AS SIMPLE PHP UPLOAD IMPLEMENTATION.
		# FEEL FREE TO USE/CHANGE/MODIFY/REDISTRIBUTE. Please notify me with improvements.
		# Developed as sample to be used in conjunction with SWFUpload (http://www.swfupload.org)
		# ABSTRACT
		# Saves SWFUpload uploaded files to a directory and intelligently reports on the process via email. 
		# SIMPLE SETUP, UP & RUNNING IN A FEW MINUTES
		# 1. Replace the original "upload.php" files from the SWFUpload v2.1.0 package with this file.
		# 2. Assign your email address to $upload_notify_email below.
		# 3. Create a PHP writable "uploads" directory as follows: swfupload/demos/uploads
	#	Setup SWFUpload Email Notify
	#	---------------	
		$upload_email_reporting = true ; 	// true or false (false turns email reporting off)
		$upload_success = true;
		$upload_notify_email = 'test.com' ;  	// enter your valid email address
		$upload_directory = '/home/musicopus1/mainwebsite_html/swfupload/uploadtemp'; // leave blank for default
		$upload_directory_from = '/home/musicopus1/mainwebsite_html/swfupload/uploadtemp/';
		$upload_directory_to = '/home/musicopus1/mainwebsite_html/swfupload/uploads/' ;
		# DEFINING $upload_directory
		# Must point to a PHP writable directory
		# See http://www.onlamp.com/pub/a/php/2003/02/06/php_foundations.html for dealing with PHP permissions
		/*
		The default directory for this script is set to "uploads" directory 
		in the same directory as the index.php of the SWFUpload demo files:
			# SWFUpload v2.1.0 Beta.zip (SWFUpload package)
				# swfupload/demos/uploads
		This 'uploads' directory may not exist with the SWFUploads package and may need created (with PHP write permissions).
		In any case, this script will send an email message concerning the status of the upload directory.
		*/
	#	PHP Email Configuration Test
	#	---------------	
		# Set to "true" to test if your server's PHP mail() function configuration works, by attempting to upload one file.
		# A simple email will be sent per upload attempt, letting you know PHP's mail() funciton is working.
		$test_php_mail_config = false ; // true or false
	#	---------------
	#	NO MODIFICATIONS REQUIRED BELOW THIS LINE
	#	---------------
	#	CREATE DEFAULT UPLOAD DIRECTY LOCATION
	#	---------------	
		If ( !$upload_directory ) {
			$upload_directory = 'uploads' ; 
			$parent_dir = array_pop(explode(DIRECTORY_SEPARATOR, dirname(__FILE__)));
			$upload_directory = substr(dirname(__FILE__), 0, strlen(dirname(__FILE__)) - strlen($parent_dir) ) . $upload_directory ; 
		}
	#	---------------	
	#	EMAIL TESTS
		If ( !$upload_notify_email ) {
			$upload_email_reporting = false ;
		}
		# Sends one email per SWFUpload attempt. 
		if ( $test_php_mail_config == true ) {
			send_mail("SWFUpload Email Test: SUCCESS!",'Be sure to set $test_php_mail_config back to false so that SWFUpload Email Notify reporting is turned on.'); 
			$upload_email_reporting = false ;
		}
	#	---------------	
	#	TEST UPLOAD DIRECTORY		
		If ( !file_exists($upload_directory) ) {	
			$upload_success = false;
			$msg = "The assigned SWFUpload directory, \"$upload_directory\" does not exist."; 
			send_mail("SWFUpload Directory Not Found: $upload_directory",$msg);
			$upload_email_reporting = false ;
		}
		if ( $upload_email_reporting == true ) {
			$uploadfile = $upload_directory. DIRECTORY_SEPARATOR . basename($_FILES['Filedata']['name']);   
			if ( !is_writable($upload_directory) ) {
				$upload_success = false;
				$msg = "The directory, \"$upload_directory\" is not writable by PHP. Permissions must be changed to upload files."; 
				send_mail("SWFUpload Directory Unwritable: $upload_directory",$msg);
				$upload_directory_writable = false ;
			} else {
				$upload_directory_writable = true ;
			}
		}
	// Work-around for setting up a session because Flash Player doesn't send the cookies
	if (isset($_POST["PHPSESSID"])) {
		session_id($_POST["PHPSESSID"]);
	}
	session_start();
	if ( !isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
		#	---------------	
		#	UPLOAD FAILURE REPORT
			if ( $upload_email_reporting == true ) {
				$upload_success = false;
				switch ($_FILES['Filedata']["error"]) {	
					case 1: $error_msg = 'File exceeded maximum server upload size of '.ini_get('upload_max_filesize').'.'; break;
					case 2: $error_msg = 'File exceeded maximum file size.'; break;
					case 3: $error_msg = 'File only partially uploaded.'; break;
					case 4: $error_msg = 'No file uploaded.'; break; 
				}
				send_mail("SWFUpload Failure: ".$_FILES["Filedata"]["name"],'PHP Error: '.$error_msg."\n\n".'Save Path: '.$uploadfile."\n\n".'$_FILES data: '."\n".print_r($_FILES,true)); 
			}
		echo "There was a problem with the upload";
		exit(0);
	} else {
		#	---------------	
		#	COPY UPLOAD SUCCESS/FAILURE REPORT
			if ( $upload_email_reporting == true AND $upload_directory_writable == true ) {
				$upload_success = false;
				if ( move_uploaded_file( $_FILES['Filedata']['tmp_name'] , $uploadfile ) ) {
				 send_mail("SWFUpload File Saved: ".$_FILES["Filedata"]["name"],'Save Path: '.$uploadfile."\n\n".'$_FILES data: '."\n".print_r($_FILES,true)); 
				}else{
				 send_mail("SWFUpload File Not Saved: ".$_FILES["Filedata"]["name"],'Save Path: '.$uploadfile."\n\n".'$_FILES data: '."\n".print_r($_FILES,true)); 
				}
			}
		echo "Flash requires that we output something or it won't fire the uploadSuccess event";
	}
	#	---------------	
	#	MAIL FUNCTION
		function send_mail($subject="Email Notify",$message="") { 
			global $upload_notify_email ; 
			$from = 'SWFUpload@mailinator.com' ; 
			$return_path = '-f '.$from ;
			#mail($upload_notify_email,$subject,$message,"From: $from\nX-Mailer: PHP/ . $phpversion()");
		}	
		$con = ""//connection string removed here
			if (!$con){
			  die('Could not connect: ' . mysql_error());
			}
			mysql_select_db("test", $con);
		if ($handle = opendir($upload_directory_from)){
			$countera = 1;
			$orig_w = 450;
			while (false !== ($file = readdir($handle))){
				$albumId = $_POST['albumId'];
				if($file != '.' && $file != '..'){
					$fileparts  = array();
					$fileparts = explode(".",$file);
					$fileext = $fileparts[1];
					list($width, $height) = getimagesize($upload_directory_from.$file);
					$orig_h = ($height/$width)*$orig_w;
					$src = '';
					if (strtolower($fileext) == 'png') {
						$src = imagecreatefrompng($upload_directory_from.$file);
					}
					else if(strtolower($fileext) == 'gif'){
						$src = imagecreatefromgif($upload_directory_from.$file);
					}
					else if(strtolower($fileext) == 'jpg'){
						$src = imagecreatefromjpeg($upload_directory_from.$file);
					}
					else{
						echo "File type not supported, .gif, .jpg or .png required!";
					}
					$now = date("Y-m-d\THis_");
					$tmp = imagecreatetruecolor($orig_w, $orig_h);
					imagecopyresampled($tmp, $src, 0,0,0,0,$orig_w,$orig_h,$width,$height);
					if (strtolower($fileext) == 'png') {
						imagepng($tmp,$upload_directory_to.$now.$file,100);
					}
					else if(strtolower($fileext) == 'gif'){
						imagegif($tmp,$upload_directory_to.$now.$file,100);
					}
					else if(strtolower($fileext) == 'jpg'){
						imagejpeg($tmp,$upload_directory_to.$now.$file,100);
					}
					else{
						echo "File type not supported, .gif, .jpg or .png required!";
					}						
					imagedestroy($tmp);
					imagedestroy($src);
					unlink($upload_directory_from.$file);
					mysql_query("INSERT INTO GALLERY_IMAGES (IMAGE_NAME, CAPTION, ALBUM_COVER, ALBUM_ID)
						VALUES ('$now$file', '', 'N', ".$albumId.")");
					$countera = $countera++;
					echo "counter=".$countera;					
				}
			}
				closedir($handle);
			}
			mysql_close($con);	

Open in new window


0
cgray1223
Asked:
cgray1223
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
The imagepng() page http://us2.php.net/manual/en/function.imagepng.php has this note:

and saving to disk, this worked:
imagepng($image,$file_location,0,NULL);

Depending on your version of PHP, you may want to add the fourth parameter or delete the third.  They were added in PHP 5.1.2 and 5.1.3.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now