thumbnail creation PHP script

 
  • To create a thumbnail, first check file entenson, and then read in the file using the imagecreatefromjpeg() or imagecreatefrompng() or imagecreatefromgif() function and can calculate the new thumbnail size.
  • imagesx() and imagesy() functions return the width and height of the image respectively.
 
 
function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|JPG|jpeg|JPEG/",$system[1])){$src_img=imagecreatefromjpeg($name);}
 

elseif (preg_match("/png|PNG/",$system[1])){$src_img=imagecreatefrompng($name);}

elseif (preg_match("/gif|GIF/",$system[1])){$src_img=imagecreatefromgif($name);}


$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
}
elseif (preg_match("/gif/",$system[1]))
{
imagegif($dst_img,$filename);
}
else{
imagejpeg($dst_img,$filename);
}

imagedestroy($dst_img);
imagedestroy($src_img);
}
Related Posts:
  • What Is a WAMP, MAMP, or LAMP? WAMP, MAMP, and LAMP are abbreviations for Windows, Apache, MySQL, andPHP,Mac, Apache, MySQL, and PHP, and Linux, Apache, MySQL, and PHP.These abbreviations describe a fully functioning setup used for developing dynamicInter… Read More
  • PHP operators are characters Artithmetic Operators OperatorDescription +Addition -Subtraction *Multiplication /Division %Modulus (remainder of a division) ++Increment --Decrement Assignment Operator Op… Read More
  • PHP error: Cannot modify header information You cannot use header() once text has been output to the browser. As your header.php include presumably outputs HTML, header() cannot be used. You can solve this in a couple ways: Move the if statement above the heade… Read More
  • How to create update or remove symbolic or soft link Linux Symbolic links , Symlink or Soft link in Unix are very important concept to understand and use in various UNIX operating systems e.g. Linux , Solaris or IBM AIX. Symlinks gives you so much power and flexibility that you can… Read More
  • PHP: How do I enable error reporting? he following enables all errors: ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(-1); See http://php.net/manual/en/errorfunc.configuration.php#ini.display-errors http://php.net/manu… Read More
  • Php tutorial-SYMBOLS ! (logical operator)!= (comparison operator)!= (inequality operator)!== (comparison operator)!== (non-identity operator)$result->fetch_assoc() function$type parameter% (modulus operator)% (wildcard character)%= (combined … Read More
  • Drupal Bootstrap Process Drupal Bootstrap Process? Drupal bootstraps itself on every request by going through a series of bootstrap phases. These phases aredefined in bootstrap.inc and proceed. Configuration Sets global variables used throughout th… Read More
  • SQL Injection Attacks This appeared to be an entirely custom application, and we had no prior knowledge of the application nor access to the source code: this was a "blind" attack. A bit of poking showed that this server ran Microsoft's IIS 6 alo… Read More
  • PHP Curl check for file existence a PHP program that downloads a pdf from a backend and save to a local drive. $url = "http://wedsite/test.pdf"; $path = "C:\\test.pdf;" downloadAndSave($url,$path); function downloadAndSave($urlS,$pathS) { $fp… Read More
  • Multiple array in php If the ID, topic and description are all in the correct order in each comma delimited string you are supplying to the script, you could use the following to create a single array key'd by the ID: … Read More
  • XMLHttpRequest-Ajax To do this, you must understand the three ways of creating an XMLHttpRequest object• IE 5: request = new ActiveXObject("Microsoft.XMLHTTP")• IE 6+: request = new ActiveXObject("Msxml2.XMLHTTP")• All others: request = new XML… Read More
  • SQL Injection Prevention the value should only be a positive integer value, since it's an id number. We do sometimes use other variables that could be a letter, or a string of text, for example, the search results pages. $variable = "0"; if (is… Read More
  • Server Side Includes A server-side include is a coding that you can include within your HTML document that will tell the web server to include other information with the document being served.    Server side includes are a handy w… Read More
  • IT company in kolkata Matrix Technologies Pvt. Ltd.8/1C, Chowringhee Lane, Room No. 2E, 2nd flKolkata7000162252 8/29 Media Software61A, S.N.Roy RdKolkata70003424479580 Metalogic Systems Pvt. LtdPlot-J1/1, Block-EP and GP, Sector-V, Salt LakeK… Read More
  • Download file using curl in php You would need to feed CURLOPT_URL the full URL to the file. Also if you want to download a file you might want to save it somewhere. Working example: $curl = curl_init(); $file = fopen("ls-lR.gz", 'w'); curl_setopt… Read More