supanoob Posted September 16, 2010 Share Posted September 16, 2010 could someone please help me adapt the following code to make it resize the image before uploading? Any help would be appreciated, thanks <?php if($_GET['submit'] == 'yes') { $image = $_FILES['image']['name']; if ($image) { $filename = stripslashes($_FILES['image']['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo 'Unknown extension!'; $errors=1; } else { $size=filesize($_FILES['image']['tmp_name']); if ($size > MAX_SIZE*1024) { echo 'You have exceeded the size limit! the limit is 500KB'; $errors=1; } $rand = rand(0,9999999); $date = DATE("d.m.y"); $image_name=$date.$id.$rand.'.'.$extension; $newname="./images/$gender/".$image_name; $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo 'Copy unsuccessfull!'; $errors=1; }}}} ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 16, 2010 Share Posted September 16, 2010 You can't resize it before uploading. Until the image has been uploaded the server doesn't have access to it. (OK, technically you could create create a Java application (not JavaScript) to resize on the clients computer before uploading, but that opens up bigger issues). You will want to upload the image and then resize it. There are a ton of resizing scripts available. Quote Link to comment Share on other sites More sharing options...
supanoob Posted September 16, 2010 Author Share Posted September 16, 2010 You can't resize it before uploading. Until the image has been uploaded the server doesn't have access to it. (OK, technically you could create create a Java application (not JavaScript) to resize on the clients computer before uploading, but that opens up bigger issues). You will want to upload the image and then resize it. There are a ton of resizing scripts available. What about using a temp image? care to share a script? i cant seem to find any Quote Link to comment Share on other sites More sharing options...
kristijan.jurkovic Posted September 17, 2010 Share Posted September 17, 2010 here you go: http://www.verot.net/php_class_upload.htm it has nice functions and great documentation as well. Quote Link to comment Share on other sites More sharing options...
chintansshah Posted September 17, 2010 Share Posted September 17, 2010 This script resize an Image into two 60px and 25px. Take a look at $newwidth you have to modify size values. <?php define ("MAX_SIZE","400");$errors=0;if($_SERVER["REQUEST_METHOD"] == "POST"){ $image =$_FILES["file"]["name"];$uploadedfile = $_FILES['file']['tmp_name']; if ($image) { $filename = stripslashes($_FILES['file']['name']); $extension = getExtension($filename); $extension = strtolower($extension);if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {echo ' Unknown Image extension ';$errors=1; }else{ $size=filesize($_FILES['file']['tmp_name']);if ($size > MAX_SIZE*1024){echo "You have exceeded the size limit";$errors=1;}if($extension=="jpg" || $extension=="jpeg" ){$uploadedfile = $_FILES['file']['tmp_name'];$src = imagecreatefromjpeg($uploadedfile);}else if($extension=="png"){$uploadedfile = $_FILES['file']['tmp_name'];$src = imagecreatefrompng($uploadedfile);}else {$src = imagecreatefromgif($uploadedfile);}list($width,$height)=getimagesize($uploadedfile);$newwidth=60;$newheight=($height/$width)*$newwidth;$tmp=imagecreatetruecolor($newwidth,$newheight);$newwidth1=25;$newheight1=($height/$width)*$newwidth1;$tmp1=imagecreatetruecolor($newwidth1,$newheight1);imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1, $width,$height);$filename = "images/". $_FILES['file']['name'];$filename1 = "images/small". $_FILES['file']['name'];imagejpeg($tmp,$filename,100);imagejpeg($tmp1,$filename1,100);imagedestroy($src);imagedestroy($tmp);imagedestroy($tmp1);}}}//If no errors registred, print the success messageif(isset($_POST['Submit']) && !$errors) { // mysql_query("update SQL statement "); echo "Image Uploaded Successfully!";}function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext;}?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 17, 2010 Share Posted September 17, 2010 care to share a script? i cant seem to find any Seriously, you can't find a PHP script that will allow you to resize images? Either 1) You didn't look or 2) you lack the basic abilities to find information that is easily available. http://lmgtfy.com/?q=php+resize+image Quote Link to comment 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.