note 62935 deleted from function.imagejpeg by nlopess

From: Date: Sun, 12 Mar 2006 22:38:31 +0000
Subject: note 62935 deleted from function.imagejpeg by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-105389@lists.php.net to get a copy of this message
Note Submitter: Yirtne ---- I'm experiencing weird behaviour with imagejpeg() function. I'm trying to upload a jpg image with a form, check wether it does not exceed 600px width, and store it in a mysql database: <?php // Get rid of magic_quotes function getsafe($tmp) { if (!get_magic_quotes_gpc()) return $tmp; else return stripslashes($tmp); } // Read the image $file = imagecreatefromjpeg($_FILES['file']['tmp_name']); // Resize and write to temporary file $sizex = imagesx($file); $sizey = imagesy($file); if ($sizex > 600) { $newsizex = 600; $newsizey = 600*$sizey/$sizex; $img = imagecreatetruecolor($newsizex, $newsizey); imagecopyresampled($img, $file, 0, 0, 0, 0, $newsizex, $newsizey, $sizex, $sizey); imagejpeg($img, $PATH_TEMPIMG); } else imagejpeg($file, $PATH_TEMPIMG); // Store in mysql database $safeimg = "'".mysql_real_escape_string(getsafe(fread(fopen($PATH_TEMPIMG, "r"), $_FILES['file']['size'])))."'"; mysql_query("INSERT INTO test (image) VALUES (".$safeimg.")") or die(mysql_error()); ?> the problem is that imagejpeg() sometimes (not always!) corrupts the picture. It still shows, but presents weird artifacts and wrong colours. please note that it does not happen always: different compression ratios for different pictures may make it show correctly or not. the server has magic_quotes on (can't change that, I'm not serveradmin) but my function getsafe() should get rid of that problem. has anybody experienced something similar before? any suggestions?

« previous php.notes (#105389) next »