cs1h Posted September 13, 2007 Share Posted September 13, 2007 Hi, does anyone know how I could upload a photo and resize (by ratio) it before saving it to disk, the following script is the one I use to upload, rename, save to server and reference to my database. <?php // Generate a random number $newname = rand(); //This is the directory where images will be saved $target = "imgs/"; $target = $target . basename( $_FILES['photo']['name']); $target = str_replace(' ', $newname, $target); // <--- then add the random number in here to replace any spaces $target = str_replace('o', $newname, $target); $target = str_replace('a', $newname, $target); $target = str_replace('e', $newname, $target); $target = str_replace('0', $newname, $target); //This gets all the other information from the form $name=$_POST['name']; $country=$_POST['menuFilesDMA']; $type=$_POST['Catagory']; $Email=$_POST['Email']; $Title=$_POST['title']; $Abstract=$_POST['message']; $Article=$_POST['messagetwo']; $pic=($target); // Connects to your Database mysql_connect("localhost", "sfdfds", "cfdsfdrd") or die(mysql_error()) ; mysql_select_db("real") or die(mysql_error()) ; //Writes the information to the database mysql_query($sql = "insert into `items` (`name`, `country`, `type`, `Email`, `Title`, `Abstract`, `Article`, `photo`) values ('$name', '$country', '$type', '$Email', '$Title', '$Abstract', '$Article', '$pic')"); //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> If any one can help it would be much appriciated because I am very stuck. Cheers Colin Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/ Share on other sites More sharing options...
Jessica Posted September 13, 2007 Share Posted September 13, 2007 If you need to resize it try using imagecopyresized Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-347723 Share on other sites More sharing options...
The Little Guy Posted September 13, 2007 Share Posted September 13, 2007 This can do 1 of 2 things: 1: Create a thumbnail of an image 2: Overwrite the large image with a smaller version of the image Tell me what you think: http://phpsnips.com/snippet.php?id=5 Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-347750 Share on other sites More sharing options...
ghr Posted September 13, 2007 Share Posted September 13, 2007 This can do 1 of 2 things: 1: Create a thumbnail of an image 2: Overwrite the large image with a smaller version of the image Tell me what you think: http://phpsnips.com/snippet.php?id=5 I've been looking to do just that so tried inserting that script into my page but I cant seem to make it work ??? Here's what I have so far: <html> <body> Car<br><br> <?php $regestration = $_POST['regestration']; $manufacturer = $_POST['manufacturer']; $model = $_POST['model']; $price = $_POST['price']; $year = $_POST['year']; $engine = $_POST['engine']; $transmission = $_POST['transmission']; $fuel = $_POST['fuel']; $mileage = $_POST['mileage']; $description = $_POST['description']; $image = $_FILES['image']; ?> <?php //define a maximum size for the uploaded images in Kb define ("MAX_SIZE","500"); //This function reads the extension of the file. //It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } //This variable is used as a flag. //The value is initialized with 0 (meaning no error found) //and it will be changed to 1 if an error occures. //If the error occures the file will not be uploaded. $errors=0; //checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '<h1>Unknown extension!</h1>'; $errors=1; } else { //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=$regestration.'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="car_images/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } else { <?php $imageDirectory = "car_images/"; $imageName = $image_name; $thumbDirectory = "car_images_new/"; $thumbWidth = 100; $quality = 80; function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){ $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.'); $type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']); eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");'); $thumbHeight = $details[1] * ($thumbWidth / $details[0]); $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]); eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'. (($type=='jpeg')?', $quality':'').');'); imagedestroy($srcImg); imagedestroy($thumbImg); } foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "data/$name"); createThumbnail("/car_images", $name, "/car_images_new", 100, 80); //100 = thumb width :: 80 = thumb quality (1-100) } ?> } } } } } //If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { echo "<h1>File Uploaded Successfully!</h1>"; } ?> <?php $htmlTable1 = ' <table width = 500 border=1 bgcolor="cyan"> <tr> <th width = "25%">Make: <td width = "75%">' ?> <?php $htmlTable2 = '</tr> <tr> <th>Model: <td>' ?> <?php $htmlTable3 = ' </tr> <tr> <th>Year: <td>' ?> <?php $htmlTable4 = '</tr> <tr> <th>Description: <td>' ?> <?php $htmlTable5 = 'cc, ' ?> <?php $htmlTableComma = ', ' ?> <?php $htmlTable6 = 'miles: <br><br>' ?> <?php $htmlTable7 = ' </td> </tr> <tr> <th>Image: <td>' ?> <?php $htmlImageLink = "<img src=".$newname.">"; ?> <?php $htmlTable8 = ' </td> </tr> </table> </body> </html>' ?> <p> You are selling the following:</p> <br><br> <table width = 500 border=1 bgcolor="orange"> <tr> <th width = "25%">Regestration: <td width = "75%"><?php print("$regestration") ?></tr> </tr> <br> <table width = 500 border=1 bgcolor="cyan"> <tr> <th width = "25%">Make: <td width = "75%"><?php print("$manufacturer") ?></tr> <tr> <th width = "25%">Model: <td width = "75%"><?php print("$model") ?></tr> <tr> <th width = "25%">Year: <td width = "75%"><?php print("$year") ?></tr> <tr> <th width = "25%">Make: <td width = "75%"><?php print("$engine cc, $transmission, $fuel, $mileage miles:<br><br>$description") ?></tr> <tr> <th width = "25%">Image: <td width = "75%"><?php print("<img src="car_images_new/.$regestration.">") ?></tr> </table> <br><br><br> <a href="Cars_for_sale.php">View Cars for Sale</a> <FORM METHOD="LINK" ACTION="Cars_for_sale.php"> <BUTTON TYPE="Submit"><FONT COLOR=GREEN>Confirm Sale</FONT></BUTTON> </FORM> <?php $ourFileName = "car_sales/$regestration.html"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fwrite($ourFileHandle, $Line1html); fwrite($ourFileHandle, $htmlTable1); fwrite($ourFileHandle, $manufacturer); fwrite($ourFileHandle, $htmlTable2); fwrite($ourFileHandle, $model); fwrite($ourFileHandle, $htmlTable3); fwrite($ourFileHandle, $year); fwrite($ourFileHandle, $htmlTable4); fwrite($ourFileHandle, $engine); fwrite($ourFileHandle, $htmlTable5); fwrite($ourFileHandle, $transmission); fwrite($ourFileHandle, $htmlTableComma); fwrite($ourFileHandle, $fuel); fwrite($ourFileHandle, $htmlTableComma); fwrite($ourFileHandle, $mileage); fwrite($ourFileHandle, $htmlTable6); fwrite($ourFileHandle, $description); fwrite($ourFileHandle, $htmlTable7); fwrite($ourFileHandle, $htmlImageLink); fwrite($ourFileHandle, $htmlTable8); fclose($ourFileHandle); ?> </body> </html> Hope someone can give me a hand ??? ??? Gareth Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-347966 Share on other sites More sharing options...
The Little Guy Posted September 13, 2007 Share Posted September 13, 2007 First, can I see your form? Second, you have a ?> after my code, remove it, you closed the php too early. Third, you are using $name, shouldn't it be $imageName in the function? From this: createThumbnail("/car_images", $name, "/car_images_new", 100, 80); To this: createThumbnail("/car_images", $imageName, "/car_images_new", 100, 80); Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-348018 Share on other sites More sharing options...
cs1h Posted September 14, 2007 Author Share Posted September 14, 2007 Hi, I have now edited my script to look like this, <?php //This function reads the extension of the file. //It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } //This variable is used as a flag. //The value is initialized with 0 (meaning no error found) //and it will be changed to 1 if an error occures. //If the error occures the file will not be uploaded. $errors=0; //checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '<h1>Unknown extension!</h1>'; $errors=1; } else { //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=rand().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="imgs/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } else { $imageDirectory = "imgs/"; $imageName = $image_name; $thumbDirectory = "imgs2/"; $thumbWidth = 100; $quality = 80; function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){ $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.'); $type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']); eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");'); $thumbHeight = $details[1] * ($thumbWidth / $details[0]); $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]); eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'. (($type=='jpeg')?', $quality':'').');'); imagedestroy($srcImg); imagedestroy($thumbImg); } foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "data/$name"); createThumbnail("/car_images", $name, "/car_images_new", 100, 80); //100 = thumb width :: 80 = thumb quality (1-100) } //This gets all the other information from the form $nameb=$_POST['name']; $country=$_POST['menuFilesDMA']; $type=$_POST['Catagory']; $Email=$_POST['Email']; $Title=$_POST['title']; $Abstract=$_POST['message']; $Article=$_POST['messagetwo']; $pic=($name); // Connects to your Database mysql_connect("localhost", "adder", "clifford") or die(mysql_error()) ; mysql_select_db("real") or die(mysql_error()) ; //Writes the information to the database mysql_query($sql = "insert into `items` (`name`, `country`, `type`, `Email`, `Title`, `Abstract`, `Article`, `photo`) values ('$nameb', '$country', '$type', '$Email', '$Title', '$Abstract', '$Article', '$pic')"); //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> but now I get the following error, Parse error: syntax error, unexpected T_ELSE in D:\Inetpub\vhost\myroho.com\httpdocs\adding.php on line 152 I am still very new to php, if anyone can help it will be much appriciated. Cheers Colin Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-348244 Share on other sites More sharing options...
watthehell Posted September 14, 2007 Share Posted September 14, 2007 There is no If for this else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> check ur code Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-348247 Share on other sites More sharing options...
redarrow Posted September 14, 2007 Share Posted September 14, 2007 This might also help ok. 1 <?php 2 if(isset($_POST['Submit'])) 3 { 4 $size = 150; // the thumbnail height 5 $filedir = 'pics/'; // the directory for the original image 6 $thumbdir = 'pics/'; // the directory for the thumbnail image 7 $prefix = 'small_'; // the prefix to be added to the original name 8 $maxfile = '2000000'; 9 $mode = '0666'; 10 $userfile_name = $_FILES['image']['name']; 11 $userfile_tmp = $_FILES['image']['tmp_name']; 12 $userfile_size = $_FILES['image']['size']; 13 $userfile_type = $_FILES['image']['type']; 14 if (isset($_FILES['image']['name'])) 15 { 16 $prod_img = $filedir.$userfile_name; 17 $prod_img_thumb = $thumbdir.$prefix.$userfile_name; 18 move_uploaded_file($userfile_tmp, $prod_img); 19 chmod ($prod_img, octdec($mode)); 20 $sizes = getimagesize($prod_img); 21 $aspect_ratio = $sizes[1]/$sizes[0]; 22 if ($sizes[1] <= $size) 23 { 24 $new_width = $sizes[0]; 25 $new_height = $sizes[1]; 26 }else{ 27 $new_height = $size; 28 $new_width = abs($new_height/$aspect_ratio); 29 } 30 $destimg=ImageCreateTrueColor($new_width,$new_height) 31 or die('Problem In Creating image'); 32 $srcimg=ImageCreateFromJPEG($prod_img) 33 or die('Problem In opening Source Image'); 34 if(function_exists('imagecopyresampled')) 35 { 36 imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) 37 or die('Problem In resizing'); 38 }else{ 39 Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) 40 or die('Problem In resizing'); 41 } 42 ImageJPEG($destimg,$prod_img_thumb,90) 43 or die('Problem In saving'); 44 imagedestroy($destimg); 45 } 46 echo ' 47 <a href="'.$prod_img.'"> 48 <img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'"> 49 </a>'; 50 }else{ 51 echo ' 52 <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data"> 53 <input type="file" name="image"><p> 54 <input type="Submit" name="Submit" value="Submit"> 55 </form>'; 56 } 57 ?> Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-348250 Share on other sites More sharing options...
cs1h Posted September 14, 2007 Author Share Posted September 14, 2007 hi, I removed the else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> and now i get the following error, Parse error: syntax error, unexpected $end in D:\Inetpub\vhost\myroho.com\httpdocs\adding.php on line 154 Does anyone know why this might be. Cheers Colin Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-348257 Share on other sites More sharing options...
watthehell Posted September 14, 2007 Share Posted September 14, 2007 check this now... <?php //This function reads the extension of the file. //It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } //This variable is used as a flag. //The value is initialized with 0 (meaning no error found) //and it will be changed to 1 if an error occures. //If the error occures the file will not be uploaded. $errors=0; //checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { /// this if is not closed i think //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '<h1>Unknown extension!</h1>'; $errors=1; } else { //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=rand().'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="imgs/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } else { $imageDirectory = "imgs/"; $imageName = $image_name; $thumbDirectory = "imgs2/"; $thumbWidth = 100; $quality = 80; function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){ $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.'); $type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']); eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");'); $thumbHeight = $details[1] * ($thumbWidth / $details[0]); $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]); eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'. (($type=='jpeg')?', $quality':'').');'); imagedestroy($srcImg); imagedestroy($thumbImg); } foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "data/$name"); createThumbnail("/car_images", $name, "/car_images_new", 100, 80); //100 = thumb width :: 80 = thumb quality (1-100) } } //********** for was not closed //This gets all the other information from the form $nameb=$_POST['name']; $country=$_POST['menuFilesDMA']; $type=$_POST['Catagory']; $Email=$_POST['Email']; $Title=$_POST['title']; $Abstract=$_POST['message']; $Article=$_POST['messagetwo']; $pic=($name); // Connects to your Database mysql_connect("localhost", "adder", "clifford") or die(mysql_error()) ; mysql_select_db("real") or die(mysql_error()) ; //Writes the information to the database mysql_query($sql = "insert into `items` (`name`, `country`, `type`, `Email`, `Title`, `Abstract`, `Article`, `photo`) values ('$nameb', '$country', '$type', '$Email', '$Title', '$Abstract', '$Article', '$pic')"); //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } } } //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-348270 Share on other sites More sharing options...
cs1h Posted September 14, 2007 Author Share Posted September 14, 2007 Hi, Thanks for the new version, the error has gone away but it is no longer uploading or saving any photos. Does anyone know why this might be, Cheers Colin Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-348327 Share on other sites More sharing options...
dlebowski Posted September 14, 2007 Share Posted September 14, 2007 Colin, I just did something similar to this recently. I may be able to help. Do you have an error log by chance? Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-348407 Share on other sites More sharing options...
cs1h Posted September 14, 2007 Author Share Posted September 14, 2007 no error log, all I'm getting is a blank screen. Any ideas ? All help is much appriciated. Cheers Colin Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-348420 Share on other sites More sharing options...
The Little Guy Posted September 14, 2007 Share Posted September 14, 2007 I edited your code in my editor to remove all the errors, here is the final result: <html> <body> Car<br><br> <?php $regestration = $_POST['regestration']; $manufacturer = $_POST['manufacturer']; $model = $_POST['model']; $price = $_POST['price']; $year = $_POST['year']; $engine = $_POST['engine']; $transmission = $_POST['transmission']; $fuel = $_POST['fuel']; $mileage = $_POST['mileage']; $description = $_POST['description']; $image = $_FILES['image']; ?> <?php //define a maximum size for the uploaded images in Kb define ("MAX_SIZE","500"); //This function reads the extension of the file. //It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } //This variable is used as a flag. //The value is initialized with 0 (meaning no error found) //and it will be changed to 1 if an error occures. //If the error occures the file will not be uploaded. $errors=0; //checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['image']['name']; //if it is not empty if ($image) { //get the original name of the file from the clients machine $filename = stripslashes($_FILES['image']['name']); //get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); //if it is not a known extension, we will suppose it is an error and will not upload the file, //otherwise we will do more tests if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { //print error message echo '<h1>Unknown extension!</h1>'; $errors=1; } else { //get the size of the image in bytes //$_FILES['image']['tmp_name'] is the temporary filename of the file //in which the uploaded file was stored on the server $size=filesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size > MAX_SIZE*1024) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=$regestration.'.'.$extension; //the new name will be containing the full path where will be stored (images folder) $newname="car_images/".$image_name; //we verify if the image has been uploaded, and print error instead $copied = copy($_FILES['image']['tmp_name'], $newname); if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } else { $imageDirectory = "car_images/"; $imageName = $image_name; $thumbDirectory = "car_images_new/"; $thumbWidth = 100; $quality = 80; function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){ $details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.'); $type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']); eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");'); $thumbHeight = $details[1] * ($thumbWidth / $details[0]); $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $details[0], $details[1]); eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'. (($type=='jpeg')?', $quality':'').');'); imagedestroy($srcImg); imagedestroy($thumbImg); } foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; $name = $_FILES["pictures"]["name"][$key]; move_uploaded_file($tmp_name, "data/$name"); createThumbnail("/car_images", $imageName, "/car_images_new", 100, 80); //100 = thumb width :: 80 = thumb quality (1-100) } } } } } } //If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { echo "<h1>File Uploaded Successfully!</h1>"; } ?> <?php $htmlTable1 = ' <table width = 500 border=1 bgcolor="cyan"> <tr> <th width = "25%">Make: <td width = "75%">' ?> <?php $htmlTable2 = '</tr> <tr> <th>Model: <td>' ?> <?php $htmlTable3 = ' </tr> <tr> <th>Year: <td>' ?> <?php $htmlTable4 = '</tr> <tr> <th>Description: <td>' ?> <?php $htmlTable5 = 'cc, ' ?> <?php $htmlTableComma = ', ' ?> <?php $htmlTable6 = 'miles: <br><br>' ?> <?php $htmlTable7 = ' </td> </tr> <tr> <th>Image: <td>' ?> <?php $htmlImageLink = "<img src=".$newname.">"; ?> <?php $htmlTable8 = ' </td> </tr> </table> </body> </html>' ?> <p> You are selling the following:</p> <br><br> <table width = 500 border=1 bgcolor="orange"> <tr> <th width = "25%">Regestration: <td width = "75%"><?php print("$regestration") ?></tr> </tr> <br> <table width = 500 border=1 bgcolor="cyan"> <tr> <th width = "25%">Make: <td width = "75%"><?php print("$manufacturer") ?></tr> <tr> <th width = "25%">Model: <td width = "75%"><?php print("$model") ?></tr> <tr> <th width = "25%">Year: <td width = "75%"><?php print("$year") ?></tr> <tr> <th width = "25%">Make: <td width = "75%"><?php print("$engine cc, $transmission, $fuel, $mileage miles:<br><br>$description") ?></tr> <tr> <th width = "25%">Image: <td width = "75%"><?php print("<img src=\"car_images_new/.$regestration\">") ?></tr> </table> <br><br><br> <a href="Cars_for_sale.php">View Cars for Sale</a> <FORM METHOD="LINK" ACTION="Cars_for_sale.php"> <BUTTON TYPE="Submit"><FONT COLOR=GREEN>Confirm Sale</FONT></BUTTON> </FORM> <?php $ourFileName = "car_sales/$regestration.html"; $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); fwrite($ourFileHandle, $Line1html); fwrite($ourFileHandle, $htmlTable1); fwrite($ourFileHandle, $manufacturer); fwrite($ourFileHandle, $htmlTable2); fwrite($ourFileHandle, $model); fwrite($ourFileHandle, $htmlTable3); fwrite($ourFileHandle, $year); fwrite($ourFileHandle, $htmlTable4); fwrite($ourFileHandle, $engine); fwrite($ourFileHandle, $htmlTable5); fwrite($ourFileHandle, $transmission); fwrite($ourFileHandle, $htmlTableComma); fwrite($ourFileHandle, $fuel); fwrite($ourFileHandle, $htmlTableComma); fwrite($ourFileHandle, $mileage); fwrite($ourFileHandle, $htmlTable6); fwrite($ourFileHandle, $description); fwrite($ourFileHandle, $htmlTable7); fwrite($ourFileHandle, $htmlImageLink); fwrite($ourFileHandle, $htmlTable8); fclose($ourFileHandle); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/69184-help-resizing-image-and-saving/#findComment-348450 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.