Php Thumbnail Example - Php

How to generate thumbnail images using php?

Snippet Code


  
Rate this page :
  [ 0 votes]

Simple php thumbnail code allows you to create thumbnail images. Here,function thumbimage() is used to create thumb sized image for the selected size. To run the below code you should create two folders to store images namely images and thumb_images.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <?php error_reporting(0); ini_set("memory_limit",-1); function thumbimage($SourceFile,$DestinationFile,$type,$fsize,$height1,$width1,$xpos,$ypos) { list($width, $height) = @getimagesize($SourceFile); $width2=$width1; $height2=$height1; $type=$type; $filter=0; $image; $image_p = imagecreatetruecolor($width1, $height1); if($type=="image/jpeg" || $type=="image/jpg" || $type=="image/pjpeg") { $image = imagecreatefromjpeg($SourceFile); $col=imagecolorallocatealpha($image,255,255,255,0); imagefill($image, 0, 0, $col); imagecopyresampled($image_p,$image, 0, 0, 0, 0,$width1,$height1,$width,$height); imagejpeg($image_p, $DestinationFile, 100); imagedestroy($image); imagedestroy($image_p); } if($type=="image/png" ||$type=="image/PNG") { $image = imagecreatefrompng($SourceFile); $col=imagecolorallocatealpha($image,255,255,255,0); imagefill($image, 0, 0, $col); imagecopyresampled($image_p,$image, 0, 0, 0, 0,$width1,$height1,$width,$height); imagejpeg($image_p, $DestinationFile, 100); imagedestroy($image); imagedestroy($image_p); } }; ?> <div align="center"> <form name="thumb" method="post" action="" enctype="multipart/form-data" onsubmit="return f();"> <b>Choose Image :</b><input type="file" accept="image/*" name="picture" id="picture" required="required"></br></br> <div id="stsize"> <b>Standard Sizes :</b> <select name="stansize" id="stansize"> <option value="10x10">10X10</option> <option value="20x20">20x20</option> <option value="50x50">50x50</option> <option value="100x100">100x100</option> </select> </div></br></br> <input type="Submit" name="Submit" value="Submit" id="submit"> </form> <?php if($_SERVER["REQUEST_METHOD"] == "POST") { global $xpos;global $ypos; $SourceFile = $_FILES["picture"]["name"]; $path="images/".$_FILES["picture"]['name']; $type = $_FILES["picture"]["type"]; $name =$_FILES["picture"]['tmp_name']; copy( $_FILES["picture"]['tmp_name'], $path ); $orgname=explode(".",$SourceFile); $newpic=rand(0,1000); $fsize=0;$xpos=0;$ypos=0; $filecount = count(glob("thumb_images/". "*.*")); $DestinationFile = 'thumb_images/resz_'.$newpic.$filecount.".jpg"; $dpath = 'thumb_images/resz_'.$newpic.$filecount.".jpg"; $size; if(isset($_POST['stansize'])) { $stansize=$_POST['stansize']; } if($stansize!="0") { $size=$_POST['stansize']; $ssize = explode("x", $size); $width=$ssize[0]; $height=$ssize[1]; } $type=$_FILES["picture"]["type"]; if(file_exists("images/" . $_FILES["picture"]["name"])) { $imgname=explode(".",$SourceFile); $imgname1=$imgname[0]."-01"; $SourceFile=$imgname1.".jpg"; $DestinationFile = 'thumb_images/resz_'.$newpic."-01.jpg"; $dpath = 'thumb_images/resz_'.$newpic."-01.jpg"; } $a=move_uploaded_file($_FILES["picture"]["tmp_name"], "images/" .$SourceFile); $SourceFile="images/" . $_FILES['picture']['name']; $alt = $imgname[0]; thumbimage ($SourceFile, $DestinationFile,$type,$fsize,$height,$width,$xpos,$ypos); echo "<div align='center' id='outputimage'><font color='green'> </font><img src='$dpath' alt='$alt'></div>"; echo " "; unlink($SourceFile); } ?> </div>

Tags


Ask Questions

Ask Question