Solved

php error with imagepng function

Posted on 2010-11-20
1
472 Views
Last Modified: 2012-05-10
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
Comment
Question by:cgray1223
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 34181178
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dump data from mysql to xls php 10 49
php output utf-8 problem 6 45
Format Date 7 22
Conditional Array Element 7 19
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question