rik72 Posted October 9, 2007 Share Posted October 9, 2007 This is intended to Upload 1 image; resize it 3 times and generate a random name for the image and save it. It's sometimes showing a blank page, sometimes showing Server Misconfiguration Error; any help is appreciated on fixing this. To be honest, i think im over doing it and using too much code, and some places not enough, but god knows! <?php $totalChar = 15; $salt = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; srand((double)microtime()*1000000); $pass=""; for ($i=0;$i<$totalChar;$i++) $pass = $pass . substr ($salt, rand() % strlen($salt), 1); $uploadedfile1 = $_FILES['uploadfile']['tmp_name']; $uploadedfile2 = $_FILES['uploadfile']['tmp_name']; $uploadedfile3 = $_FILES['uploadfile']['tmp_name']; $src1 = touch($uploadedfile1); $src2 = touch($uploadedfile2); $src3 = touch($uploadedfile3); list($width1,$height1)=getimagesize($uploadedfile1); list($width2,$height2)=getimagesize($uploadedfile2); list($width3,$height3)=getimagesize($uploadedfile3); $newwidth1=600; $newwidth2=200; $newwidth3=100; $newheight1=($height1/$width1)*600; $newheight2=($height2/$width2)*200; $newheight3=($height3/$width3)*100; $tmp1=imagecreatetruecolor($newwidth1,$newheight1); $tmp2=imagecreatetruecolor($newwidth2,$newheight2); $tmp3=imagecreatetruecolor($newwidth3,$newheight3); imagecopyresampled($tmp1,$src1,0,0,0,0,$newwidth1,$newheight1,$width1,$height1); imagecopyresampled($tmp2,$src2,0,0,0,0,$newwidth2,$newheight2,$width2,$height2); imagecopyresampled($tmp3,$src3,0,0,0,0,$newwidth3,$newheight3,$width3,$height3); $filename1 = "upimg/$pass" . $_FILES["file"]["name"]; $filename2 = "thimg/$pass" . $_FILES["file"]["name"]; $filename3 = "avimg/$pass" . $_FILES["file"]["name"]; $name = $_FILES["file"]["name"]; imagejpeg($tmp1,$filename1,100); imagejpeg($tmp2,$filename2,100); imagejpeg($tmp3,$filename3,100); imagedestroy($src1); imagedestroy($tmp1); imagedestroy($src2); imagedestroy($tmp2); imagedestroy($src3); imagedestroy($tmp3); echo "$pass $name"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/ Share on other sites More sharing options...
MadTechie Posted October 9, 2007 Share Posted October 9, 2007 Heres a cleaned up version UNTESTED, it should save memory, basically it resized to the largest size then resizes the last one to next largest size, etc, this save it resizing one large image each time.. also i have broken it down to clean up as it goes.. i removed the touch as i can see how that would of worked, as your using imagejpeg, i used imagecreatefromjpeg, which mean this will only work with jpegs.. i hope it makes sense <?php $totalChar = 15; $salt = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; srand((double)microtime()*1000000); $pass=""; for ($i=0;$i<$totalChar;$i++) $pass = $pass . substr ($salt, rand() % strlen($salt), 1); $uploadedfile = $_FILES['uploadfile']['tmp_name']; list($width,$height)=getimagesize($uploadedfile); $newwidth1=600; $newwidth2=200; $newwidth3=100; $newheight1=($height/$width)*600; $newheight2=($height/$width)*200; $newheight3=($height/$width)*100; #1 $src = imagecreatefromjpeg($uploadedfile);//Create from JPEG! $tmp1=imagecreatetruecolor($newwidth1,$newheight1); imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height); $filename = "upimg/$pass" . $_FILES["file"]["name"]; imagejpeg($tmp1,$filename,100); imagedestroy($src); #2 $tmp2=imagecreatetruecolor($newwidth2,$newheight2); imagecopyresampled($tmp2,$tmp1,0,0,0,0,$newwidth2,$newheight2,$newwidth1,$newheight1); $filename = "thimg/$pass" . $_FILES["file"]["name"]; imagejpeg($tmp2,$filename,100); imagedestroy($tmp1); #3 $tmp3=imagecreatetruecolor($newwidth3,$newheight3); imagecopyresampled($tmp3,$tmp2,0,0,0,0,$newwidth3,$newheight3,$newwidth2,$newheight2); $filename = "avimg/$pass" . $_FILES["file"]["name"]; imagejpeg($tmp3,$filename,100); imagedestroy($tmp2); imagedestroy($tmp3); $name = $_FILES["file"]["name"]; echo "$pass $name"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-365789 Share on other sites More sharing options...
rik72 Posted October 10, 2007 Author Share Posted October 10, 2007 You sir, are a legend! All i need really now is; a way of adapting the script to work with GIF's and a way of checking if the file exists so it wont overwrite. Anyone that can offer me any pointers at all? Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-365832 Share on other sites More sharing options...
MadTechie Posted October 10, 2007 Share Posted October 10, 2007 quite a few updates again untested, <?php $fileName = $_FILES["file"]["name"]; // get the file extension first $ext = substr(strrchr($fileName, "."), 1); // make the random file name do{ $randName = md5(rand() * time()); $pass = "$randName.$ext"; $Check = (file_exists("upimg/$pass") || file_exists("thimg/$pass") || file_exists("avimg/$pass")); }while( $Check ) $uploadedfile = $_FILES['uploadfile']['tmp_name']; list($width,$height)=getimagesize($uploadedfile); $newwidth1=600; $newwidth2=200; $newwidth3=100; $newheight1=($height/$width)*600; $newheight2=($height/$width)*200; $newheight3=($height/$width)*100; switch($_FILES['uploadfile']['type']) { case "image/gif": $src = imagecreatefromgif($uploadedfile);//Create from GIF! $type = "gif"; break; case "image/jpg": case "image/jpeg": $src = imagecreatefromjpeg($uploadedfile);//Create from JPEG! $type = "jpg"; break; } #1 //$src = imagecreatefromjpeg($uploadedfile);//Create from JPEG! $tmp1=imagecreatetruecolor($newwidth1,$newheight1); imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height); $filename = "upimg/$pass";// . $_FILES["file"]["name"]; if($type == "gif") { imagegif($tmp1,$filename,100); }elseif($type == "jpg"){ imagejpeg($tmp1,$filename,100); } imagedestroy($src); #2 $tmp2=imagecreatetruecolor($newwidth2,$newheight2); imagecopyresampled($tmp2,$tmp1,0,0,0,0,$newwidth2,$newheight2,$newwidth1,$newheight1); $filename = "thimg/$pass";// . $_FILES["file"]["name"]; if($type == "gif") { imagegif($tmp2,$filename,100); }elseif($type == "jpg"){ imagejpeg($tmp2,$filename,100); } imagedestroy($tmp1); #3 $tmp3=imagecreatetruecolor($newwidth3,$newheight3); imagecopyresampled($tmp3,$tmp2,0,0,0,0,$newwidth3,$newheight3,$newwidth2,$newheight2); $filename = "avimg/$pass";// . $_FILES["file"]["name"]; if($type == "gif") { imagegif($tmp3,$filename,100); }elseif($type == "jpg"){ imagejpeg($tmp3,$filename,100); } imagedestroy($tmp2); imagedestroy($tmp3); //$name = $_FILES["file"]["name"]; echo "$pass"; // $name"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-365847 Share on other sites More sharing options...
rik72 Posted October 10, 2007 Author Share Posted October 10, 2007 Parse error: syntax error, unexpected T_VARIABLE, expecting ';' I'm getting this error, yet see no issues myself.. Its coming on line 22, which is this: $Check = (file_exists("upimg/$pass") || file_exists("thimg/$pass") || file_exists("avimg/$pass")); }while( $Check ) $uploadedfile = $_FILES["uploadfile"]["tmp_name"]; The center line is line 22 Thanks Rik Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-366208 Share on other sites More sharing options...
MadTechie Posted October 10, 2007 Share Posted October 10, 2007 a do-while (required the so change to }while( $Check ); Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-366530 Share on other sites More sharing options...
rik72 Posted October 10, 2007 Author Share Posted October 10, 2007 Sorry, so simple to fix! but now getting these when uploading gif;(JPG works fine!) Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in * Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/var/tmp/php69wIuG' is not a valid JPEG file in * Warning: imagecopyresampled(): supplied argument is not a valid Image resource in * Warning: imagedestroy(): supplied argument is not a valid Image resource in * Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-366552 Share on other sites More sharing options...
MadTechie Posted October 10, 2007 Share Posted October 10, 2007 ok thats weird.. add the following to the start var_dump($_FILES); die; and upload the gif, and post back the results the code above will kill the script but should highlight the problem Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-366561 Share on other sites More sharing options...
rik72 Posted October 10, 2007 Author Share Posted October 10, 2007 array(1) { ["uploadfile"]=> array(5) { ["name"]=> string(16) "t9newpreview.gif" ["type"]=> string(9) "image/gif" ["tmp_name"]=> string(18) "/var/tmp/phpDnZaKE" ["error"]=> int(0) ["size"]=> int(82766) } } Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-366588 Share on other sites More sharing options...
MadTechie Posted October 10, 2007 Share Posted October 10, 2007 ok you can remove those 2 lines.. can you post the code you have.. as it should be working! Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-366593 Share on other sites More sharing options...
rik72 Posted October 10, 2007 Author Share Posted October 10, 2007 $fileName = $_FILES["uploadfile"]["name"]; // get the file extension first $ext = substr(strrchr($fileName, "."), 1); // make the random file name do{ $randName = md5(rand() * time()); $pass = "$randName.$ext"; $Check = (file_exists("upimg/$pass") || file_exists("thimg/$pass") || file_exists("avimg/$pass")); }while( $Check ); $uploadedfile = $_FILES["uploadfile"]["tmp_name"]; list($width,$height)=getimagesize($uploadedfile); $newwidth1=600; $newwidth2=200; $newwidth3=100; $newheight1=($height/$width)*600; $newheight2=($height/$width)*200; $newheight3=($height/$width)*100; switch($_FILES['uploadfile']['type']) { case "image/gif": $src = imagecreatefromgif($uploadedfile);//Create from GIF! $type = "gif"; break; case "image/jpg": case "image/jpeg": $src = imagecreatefromjpeg($uploadedfile);//Create from JPEG! $type = "jpg"; break; } #1 $src = imagecreatefromjpeg($uploadedfile);//Create from JPEG! $tmp1=imagecreatetruecolor($newwidth1,$newheight1); imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height); $filename = "upimg/$pass";// . $_FILES["uploadfile"]["name"]; if($type == "gif") { imagegif($tmp1,$filename,100); }elseif($type == "jpg"){ imagejpeg($tmp1,$filename,100); } imagedestroy($src); #2 $tmp2=imagecreatetruecolor($newwidth2,$newheight2); imagecopyresampled($tmp2,$tmp1,0,0,0,0,$newwidth2,$newheight2,$newwidth1,$newheight1); $filename = "thimg/$pass";// . $_FILES["uploadfile"]["name"]; if($type == "gif") { imagegif($tmp2,$filename,100); }elseif($type == "jpg"){ imagejpeg($tmp2,$filename,100); } imagedestroy($tmp1); #3 $tmp3=imagecreatetruecolor($newwidth3,$newheight3); imagecopyresampled($tmp3,$tmp2,0,0,0,0,$newwidth3,$newheight3,$newwidth2,$newheight2); $filename = "avimg/$pass";// . $_FILES["uploadfile"]["name"]; if($type == "gif") { imagegif($tmp3,$filename,100); }elseif($type == "jpg"){ imagejpeg($tmp3,$filename,100); } imagedestroy($tmp2); imagedestroy($tmp3); $name = $_FILES["uploadfile"]["name"]; } Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-366604 Share on other sites More sharing options...
MadTechie Posted October 10, 2007 Share Posted October 10, 2007 ahhh HA #1 $src = imagecreatefromjpeg($uploadedfile);//Create from JPEG! should be #1 //$src = imagecreatefromjpeg($uploadedfile);//Create from JPEG! Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-366606 Share on other sites More sharing options...
rik72 Posted October 11, 2007 Author Share Posted October 11, 2007 Thanks, works great again! Quote Link to comment https://forums.phpfreaks.com/topic/72540-solved-image-upload-resize/#findComment-366614 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.