Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1. function DeleteThumbAndImage($name) {
  2.   unlink('Imgs/'.preg_replace('/(.*)\.[^.]*/','\\1_thumb.jpg',$name));
  3.   unlink('Imgs/'.$name);
  4. }
  5.  
  6. function CreateThumbAndCopyImage($fpath,$name) {
  7.   $tmp = 'Imgs/'.$name;
  8.   if (is_uploaded_file($fpath)) { if (@move_uploaded_file($fpath,$tmp) == false) die('image_process_error_1'); }
  9.   else if (rename($fpath,$tmp) == false) die('image_process_error_1');
  10.   $fpath = $tmp;
  11.   $nameonly = preg_replace('/(.*)\.[^\.]*/','\\1',$name);
  12.   $nameextonly = substr($name,strlen($nameonly));
  13.   switch (strtolower($nameextonly)) {
  14.     case '.jpg':
  15.     case '.jpeg':
  16.       $sourceimg = imagecreatefromjpeg($fpath);
  17.       break;
  18.     case '.png':
  19.       $sourceimg = imagecreatefrompng($fpath);
  20.       break;
  21.     case '.gif':
  22.       $sourceimg = imagecreatefromgif($fpath);
  23.       break;
  24.   }
  25.   if (!$sourceimg) die('image_process_error_2');
  26.  
  27.   $sourcewidth = imagesx($sourceimg);
  28.   $sourceheight = imagesy($sourceimg);
  29.  
  30.   $targetwidthlimit = 85;
  31.   $targetheightlimit = 75;
  32.  
  33.   if (($targetwidthlimit / $sourcewidth) < ($targetheightlimit / $sourceheight)) $sizingpercent = $targetwidthlimit/$sourcewidth;
  34.   else $sizingpercent = $targetheightlimit/$sourceheight;
  35.  
  36.   $targetwidth = round($sourcewidth*$sizingpercent);
  37.   $targetheight = round($sourceheight*$sizingpercent);
  38.  
  39.   $destimg = imagecreatetruecolor($targetwidth,$targetheight);
  40.   if (!$destimg) {
  41.     @imagedestroy($sourceimg);
  42.     die('image_process_error_3');
  43.   }
  44.   imagecopyresampled($destimg,$sourceimg,0,0,0,0,$targetwidth,$targetheight,$sourcewidth,$sourceheight);
  45.   @mkdir('Imgs');
  46.   if (imagejpeg($destimg,'Imgs/'.$nameonly.'_thumb.jpg') == false) {
  47.     @imagedestroy($destimg);
  48.     @imagedestroy($sourceimg);
  49.     die('image_process_error_4');
  50.   }
  51.   @imagedestroy($destimg);
  52.   @imagedestroy($sourceimg);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement