Advertisement
tjromano

convert

Nov 10th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.03 KB | None | 0 0
  1. <?php
  2. //$source_file = preg_replace("/(http:\/\/www.magicmurals.com)|(https:\/\/www.magicmurals.com)/", "/web/magicmurals/stage/var/cache/images/",$_GET['src']);
  3. $source_file = preg_replace("/(http:\/\/foo.magicmurals.com)|(https:\/\/w.magicmurals.com)/", "/web/magicmurals/stage/var/cache/images/",$_GET['src']);
  4. $source_file = $_GET['src'];
  5. $source_file = preg_replace("/(http:\/\/staging.magicmurals.com)|(https:\/\/www.magicmurals.com)/", "/web/magicmurals/prod",$_GET['src']);
  6. $grayscale = $_GET['g'];
  7. $vertical = $_GET['v'];
  8. $horizantal = $_GET['h'];
  9. $cartImage = $_GET['c'];
  10. $crop = $_GET['crop'];
  11. $sepia = $_GET['s'];
  12. $watermark = $_GET['wm'];
  13. $trg = $_GET['trg'];
  14. $resize = $_GET['resize'];
  15.  
  16. //get file type
  17. $source_file_type = image_type_to_mime_type(exif_imagetype($source_file));
  18.  
  19. $im = ImageCreateFromJpeg($source_file);
  20. if( $source_file_type == 'image/jpeg') $im = ImageCreateFromJpeg($source_file);
  21. if( $source_file_type == 'image/gif') $im = @imagecreatefromgif($source_file);
  22. if( $source_file_type == 'image/png') $im = @imagecreatefrompng($source_file);
  23.  
  24. function grayscale(&$img) {
  25.     $imggw = imagesx($img);
  26.     $imggh = imagesy($img);
  27.  
  28.     for ($i=0; $i<$imggw; $i++){
  29.         for ($j=0; $j<$imggh; $j++) {
  30.        
  31.             $rgb = ImageColorAt($img, $i, $j);
  32.                
  33.             $rr = ($rgb >> 16) & 0xFF;
  34.             $gg = ($rgb >> 8) & 0xFF;
  35.             $bb = $rgb & 0xFF;
  36.                
  37.             $g = round(($rr + $gg + $bb) / 3);
  38.             $val = imagecolorallocate($img, $g, $g, $g);
  39.             imagesetpixel ($img, $i, $j, $val);
  40.         }
  41.     }
  42. }
  43.  
  44. function flipVertical(&$img) {
  45.     $size_x = imagesx($img);
  46.     $size_y = imagesy($img);
  47.     $temp = imagecreatetruecolor($size_x, $size_y);
  48.     $x = imagecopyresampled($temp, $img, 0, 0, 0, ($size_y-1), $size_x, $size_y, $size_x, 0-$size_y);
  49.     if ($x) {
  50.         $img = $temp;
  51.     } else {
  52.         die("Unable to flip image");
  53.     }
  54. }
  55.  
  56. function flipHorizontal(&$img) {
  57.     $size_x = imagesx($img);
  58.     $size_y = imagesy($img);
  59.     $temp = imagecreatetruecolor($size_x, $size_y);
  60.     $x = imagecopyresampled($temp, $img, 0, 0, ($size_x-1), 0, $size_x, $size_y, 0-$size_x, $size_y);
  61.         if ($x) {
  62.             $img = $temp;
  63.     } else {
  64.         die("Unable to flip image");
  65.     }
  66. }
  67.  
  68. function resizeCartImg(&$img) {
  69.     $size_x = 248;
  70.     $ratio = 248/imagesx($img);
  71.     $size_y = imagesy($img)*$ratio;
  72.     $temp = imagecreatetruecolor($size_x, $size_y);
  73.     $x = imagecopyresampled($temp, $img, 0, 0, 0, 0, $size_x, $size_y, imagesx($img), imagesy($img));
  74.         if ($x) {
  75.             $img = $temp;
  76.     } else {
  77.         die("Unable to resize image");
  78.     }
  79. }
  80.  
  81. function resizeCropImg(&$img) {
  82.     $size_x = 573;
  83.     $ratio = 573/imagesx($img);
  84.     $size_y = imagesy($img)*$ratio;
  85.     $temp = imagecreatetruecolor($size_x, $size_y);
  86.     $x = imagecopyresampled($temp, $img, 0, 0, 0, 0, $size_x, $size_y, imagesx($img), imagesy($img));
  87.         if ($x) {
  88.             $img = $temp;
  89.     } else {
  90.         die("Unable to resize image");
  91.     }
  92. }
  93.  
  94. function sepiaImg(&$img) {
  95.     imagefilter($img,IMG_FILTER_GRAYSCALE);
  96.     imagefilter($img,IMG_FILTER_COLORIZE,100,50,0);
  97.  
  98. }
  99.  
  100. function resizeImg(&$img) {
  101.  
  102.     $size_x = 573;
  103.     $ratio = 573/imagesx($img);
  104.     $size_y = imagesy($img)*$ratio;
  105.     $temp = imagecreatetruecolor($size_x, $size_y);
  106.     $x = imagecopyresampled($temp, $img, 0, 0, 0, 0, $size_x, $size_y, imagesx($img), imagesy($img));
  107.         if ($x) {
  108.             $img = $temp;
  109.     } else {
  110.         die("Unable to resize image");
  111.     }
  112.  
  113. }
  114.  
  115. function watermarkImg(&$img) {
  116. // Open the watermark
  117. $watermark = new Imagick();
  118. $watermark->readImage("/web/magicmurals/prod/images/watermarkMM.png");
  119.     $size_x = imagesx($img);
  120.     $size_y = imagesy($img);
  121.     $temp = imagecreatetruecolor($size_x, $size_y);
  122.     $temp->compositeImage($watermark, imagick::COMPOSITE_OVER, (((($temp->getImageWidth()) - ($watermark->getImageWidth())))/2), (((($temp->getImageHeight()) - ($watermark->getImageHeight())))* .96));
  123.     $x = imagecopyresampled($temp, $img, 0, 0, 0, 0, $size_x, $size_y, imagesx($img), imagesy($img));
  124.         if ($x) {
  125.             $img = $temp;
  126.     } else {
  127.         die("Unable to resize image");
  128.     }
  129.  
  130. }
  131.  
  132. if($grayscale=="1")grayscale($im);
  133. if($vertical=="1")flipVertical($im);
  134. if($horizantal=="1")flipHorizontal($im);
  135. if($cartImage=="1")resizeCartImg($im);
  136. if($crop=="1")resizeCropImg($im);
  137. if($sepia=="1") sepiaImg($im);
  138. if($resize=="1") resizeImg($im);
  139. if($watermark=="10") watermarkImg($im);
  140.  
  141. if ($watermark != '0') {
  142. $img_name = basename($_GET['src']);
  143.  
  144. $strUnique = sprintf("%u", ip2long($_SERVER['REMOTE_ADDR'])) . floor(microtime(true));
  145. $file = "/web/magicmurals/prod/images/output-".$img_name.'_'.$strUnique."_".$_GET['s']."_".$_GET['trg']."_".$_GET['h']."_".$_GET['g'].".jpg";
  146. imagejpeg($im, $file);
  147.  
  148. $img = new Imagick();
  149. $img->readImage($file);
  150.  
  151. $watermark = new Imagick();
  152. $watermark->readImage("/web/magicmurals/prod/images/watermarkMM.png");
  153. $img->compositeImage($watermark, imagick::COMPOSITE_OVER, (((($img->getImageWidth()) - ($watermark->getImageWidth())))/2), (((($img->getImageHeight()) - ($watermark->getImageHeight())))* .96));
  154.  
  155. header("Content-Type: image/" . $img->getImageFormat());
  156. echo $img;
  157. } else {
  158.  
  159.  
  160.  header('Content-type: image/jpeg');
  161. imagejpeg($im);
  162. }
  163.  
  164. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement