File Upload and generate two thumbnails of several sizes

Source: Internet
Author: User
Tags imagecopy imagejpeg
<?php//----------------------------------------- start edit here ---------------------------------------------//$script_location = "http://up.online.cm"; // location fo the script$maxlimit = 9048576; // maxim image limit$folder = "images"; // folder where to save images// requirements$minwidth = 500; // minim width$minheight = 500; // minim height$maxwidth = 2560; // maxim width$maxheight = 1920; // maxim height// allowed extensions$extensions = array('.png', '.gif', '.jpg', '.jpeg','.PNG', '.GIF', '.JPG', '.JPEG');//----------------------------------------- end edit here ---------------------------------------------//// check that we have a fileif((!empty($_FILES["uploadfile"])) && ($_FILES['uploadfile']['error'] == 0)) {// check extension$extension = strrchr($_FILES['uploadfile']['name'], '.');if (!in_array($extension, $extensions)){echo 'wrong file format, alowed only .png , .gif, .jpg, .jpeg<script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';} else {// get file size$filesize = $_FILES['uploadfile']['size'];// check filesizeif($filesize > $maxlimit){ echo "File size is too big.";} else if($filesize < 1){ echo "File size is empty.";} else {// temporary file$uploadedfile = $_FILES['uploadfile']['tmp_name'];// capture the original size of the uploaded imagelist($width,$height) = getimagesize($uploadedfile);// check if image size is lowerif($width < $minwidth || $height < $minheight){ echo 'Image is to small. Required minimum '.$minwidth.'x'.$minheight.'<script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';} else if($width > $maxwidth || $height > $maxheight){ echo 'Image is to big. Required maximum '.$maxwidth.'x'.$maxheight.'<script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';} else {// all characters lowercase$filename = strtolower($_FILES['uploadfile']['name']);// replace all spaces with _$filename = preg_replace('/\s/', '_', $filename);// extract filename and extension$pos = strrpos($filename, '.'); $basename = substr($filename, 0, $pos); $ext = substr($filename, $pos+1);// get random number$rand = time();// image name$image = $basename .'-'. $rand . "." . $ext;// check if file exists$check = $folder . '/' . $image;if (file_exists($check)) {echo 'Image already exists';} else {// check if it's animate gif$frames = exec("identify -format '%n' ". $uploadedfile ."");if ($frames > 1) {// yes it's animate image// copy original imagecopy($_FILES['uploadfile']['tmp_name'], $folder . '/' . $image);// orignal image location$write_image = $folder . '/' . $image;//ennable formecho '<br /><input type="text" name="location" value="[IMG]'.$script_location.''.$write_image.'[/IMG]" class="location corners" /><script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';} else {// create an image from it so we can do the resize switch($ext){  case "gif":$src = imagecreatefromgif($uploadedfile);  break;  case "jpg":$src = imagecreatefromjpeg($uploadedfile);  break;  case "jpeg":$src = imagecreatefromjpeg($uploadedfile);  break;  case "png":$src = imagecreatefrompng($uploadedfile);  break; }// copy original imagecopy($_FILES['uploadfile']['tmp_name'], $folder . '/' . $image);// orignal image location$write_image = $folder . '/' . $image;// create first thumbnail image - resize original to 80 width x 80 height pixels $newheight = ($height/$width)*80;$newwidth = 80;$tmp=imagecreatetruecolor($newwidth,$newheight);imagealphablending($tmp, false);imagesavealpha($tmp,true);$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);// write thumbnail to disk$write_thumbimage = $folder .'/thumb-'. $image; switch($ext){  case "gif":imagegif($tmp,$write_thumbimage);  break;  case "jpg":imagejpeg($tmp,$write_thumbimage,100);  break;  case "jpeg":imagejpeg($tmp,$write_thumbimage,100);  break;  case "png":imagepng($tmp,$write_thumbimage);  break; }// create second thumbnail image - resize original to 125 width x 125 height pixels $newheight = ($height/$width)*125;$newwidth = 125;$tmp=imagecreatetruecolor($newwidth,$newheight);imagealphablending($tmp, false);imagesavealpha($tmp,true);$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);// write thumbnail to disk$write_thumb2image = $folder .'/thumb2-'. $image; switch($ext){  case "gif":imagegif($tmp,$write_thumb2image);  break;  case "jpg":imagejpeg($tmp,$write_thumb2image,100);  break;  case "jpeg":imagejpeg($tmp,$write_thumb2image,100);  break;  case "png":imagepng($tmp,$write_thumb2image);  break; }// create third thumbnail image - resize original to 125 width x 125 height pixels $newheight = ($height/$width)*250;$newwidth = 250;$tmp=imagecreatetruecolor($newwidth,$newheight);imagealphablending($tmp, false);imagesavealpha($tmp,true);$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);// write thumbnail to disk$write_thumb3image = $folder .'/thumb3-'. $image; switch($ext){  case "gif":imagegif($tmp,$write_thumb3image);  break;  case "jpg":imagejpeg($tmp,$write_thumb3image,100);  break;  case "jpeg":imagejpeg($tmp,$write_thumb3image,100);  break;  case "png":imagepng($tmp,$write_thumb3image);  break; }// all is done. clean temporary filesimagedestroy($src);imagedestroy($tmp);// image previewecho "<br /><input type='text' name='location' value='[IMG]".$script_location."/". $write_thumbimage ."[/IMG]' class='location corners' /><br /><br /><br /><input type='text' name='location' value='[IMG]".$script_location."/". $write_thumb2image ."[/IMG]' class='location corners' /><br /><br /><br /><input type='text' name='location' value='[IMG]".$script_location."/". $write_thumb3image ."[/IMG]' class='location corners' /><br /><br /><br /><input type='text' name='location' value='[IMG]".$script_location."/".$write_image."[/IMG]' class='location corners' /><script language='javascript' type='text/javascript'>window.top.window.formEnable();</script><div class='clear'></div>";  }}  }}// database connectioninclude('inc/db.inc.php');// insert into mysql database and show success messagemysql_query("INSERT INTO `image_upload` (`id`, `image`, `thumbnail`, `thumbnail2`, `thumbnail3` ) VALUES (NULL, '". $image ."', 'thumb-". $image ."', 'thumb2-". $image ."', 'thumb3-". $image ."')");  }// error all fileds must be filled} else {echo '<div class="wrong">You must to fill all fields!</div>'; }?>

Image Uploader Ajax uploads and generates thumbnails. The original version is a well-known overseas Image Upload program. It adopts the non-refreshing Ajax upload method. After the upload is successful, four thumbnails of different sizes can be generated, you can adjust the parameters by yourself. The demo results can be viewed on the homepage of this site. At the same time, it can connect to the database and save the image information to the database.
To upload images on this site, you only need to click browse to select images and the images will be automatically uploaded.
After uploading images on this site, nine newly uploaded images will be displayed on the home page.
The upload time varies depending on the image size. Please wait.
For example, wait a few minutes before uploading. click the logo on this site to refresh this page. if the image you uploaded appears at the lower part of the home page. verify that the upload is successful. you can draw other image addresses based on the rules described below.
This site is for testing and not for commercial use. Therefore, the bandwidth is limited. If the upload fails, upload it again.
The homepage displays the first thumbnail in the format of http://up.online.cm/images/thumb-source image address.
According to the previous description, the four images are respectively 1. Thumb-source image address; 2. thumb2-source image address; 3. thumb3-source image address; 4. Source image address.
If you know the address of an image, you can know the address of another image.
The size of the four images on this site is limited to: 80px 125px 250px source image.
Images uploaded on this site can be in the following formats: GIF, JPG, and PNG.
The size limit of uploaded images on this site is: Minimum size cannot be less than 100px x100px maximum size cannot be greater than 2560px x 1920px
If the configuration on this site does not meet your requirements, please download the source code and install it in the space.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.