src/AD13/ImageBundle/Service/Image.php line 299

Open in your IDE?
  1. <?php
  2. namespace AD13\ImageBundle\Service;
  3. class Image {
  4.     var $mime false;
  5.     var $type false;
  6.     var $base_dir '';
  7.     public function __construct() {
  8.         $this->base_dir getcwd();
  9.         $this->doConfigTest();
  10.     }
  11.     private function doConfigTest() {
  12.         $test_dirs = array(
  13.             'ds',
  14.             'ds/custom',
  15.             'ds/lock',
  16.             'ds/orig',
  17.             'ds/th'
  18.         );
  19.         foreach($test_dirs as $dir) {
  20.             $dir_path $this->base_dir.'/'.$dir;
  21.             if(!is_dir($dir_path)) {
  22.                 if(!mkdir($dir_path)) {
  23.                     trigger_error('IMAGE:: '.$dir_path.' se nepodarilo vytvorit'E_USER_ERROR);
  24.                 }
  25.             }
  26.         }
  27.     }
  28.     public function setMime($mime) {
  29.         $this->mime $mime;
  30.     }
  31.     public function setType($type) {
  32.         switch($type) {
  33.             default:
  34.                 trigger_error('IMAGE:: neznamy typ obrazku: '.$typeE_USER_ERROR);
  35.             break;
  36.             case 'JPG':
  37.             case 'jpg':
  38.             case 'jpeg':
  39.                 $this->type = array('extension' => 'jpg''type' => 'image/jpeg''image_create_func' => 'imagecreatefromjpeg''image_save_func' => 'imagejpeg');
  40.             break;
  41.             case 'png':
  42.                 $this->type = array('extension' => 'png''type' => 'image/png''image_create_func' => 'imagecreatefrompng''image_save_func' => 'imagepng');
  43.             break;
  44.         }
  45.     }
  46.     public function setTypeByName($file_name) {
  47.         $fn_expl explode('.',$file_name);
  48.         $ext $fn_expl[count($fn_expl)-1];
  49.         $this->setType($ext);
  50.     }
  51.     public function persist() {
  52.         if($this->mime) {
  53.             $new_file_name $this->getFileName();
  54.             $orig_img imagecreatefromstring($this->mime);
  55.             list($width$height) = array(imagesx($orig_img), imagesy($orig_img));
  56.             //save original
  57.             if($width 3840) {
  58.                 file_put_contents($this->base_dir.'/ds/orig/'.$new_file_name.'.'.$this->type['extension'] , $this->getResize(3840,0));
  59.             } else {
  60.                 file_put_contents($this->base_dir.'/ds/orig/'.$new_file_name.'.'.$this->type['extension'] , $this->mime);
  61.             }
  62.             //save thumbnail
  63.             file_put_contents($this->base_dir.'/ds/th/'.$new_file_name.'.'.$this->type['extension'] , $this->getResize(300,0));
  64.             return array(
  65.                 'new_file_name' => $new_file_name,
  66.                 'extension' => $this->type['extension'],
  67.                 'width' => $width,
  68.                 'height' => $height
  69.             );
  70.         } else {
  71.             trigger_error('IMAGE:: obsah je prázdný'E_USER_ERROR);
  72.         }
  73.     }
  74.     public function getResize($w 0,$h 0,$method 'exact'$compression 95) {
  75.         if($this->mime) {
  76.             $src imagecreatefromstring($this->mime);
  77.             //get image size from $src handle
  78.             list($width$height) = array(imagesx($src), imagesy($src));
  79.             switch($method) {
  80.                 case 'exact':
  81.                     $aspect = ($width/$height);
  82.                     if($w == 0)
  83.                         $w = ($h*$aspect);
  84.                     if($h == 0)
  85.                         $h = ($w/$aspect);
  86.                     $tmp imagecreatetruecolor($w$h);
  87.                     if($this->type['type'] == 'image/png') {
  88.                         //allow transparency for pngs
  89.                         imagealphablending($tmpfalse);
  90.                         imagesavealpha($tmptrue);
  91.                     }
  92.                     imagecopyresampled($tmp$src0000$w$h$width$height);
  93.                     break;
  94.                 case 'crop-best':
  95.                     $new_aspect = ($w/$h);
  96.                     if($w $width or $h $height) {
  97.                         $crop_w 0;
  98.                         $crop_h 0;
  99.                         $end_found false;
  100.                         while(!$end_found) {
  101.                             $crop_w++;
  102.                             $crop_h = ($crop_w/$new_aspect);
  103.                             if($crop_w >= $width)
  104.                                 $end_found true;
  105.                             if($crop_h >= $height)
  106.                                 $end_found true;
  107.                         }
  108.                         //posuny
  109.                         $move_x = ($width-$crop_w)/2;
  110.                         $move_y = ($height-$crop_h)/2;
  111.                         $tmp_cut_1 imagecreatetruecolor($crop_w$crop_h);
  112.                         if($this->type['type'] == 'image/png') {
  113.                             //allow transparency for pngs
  114.                             imagealphablending($tmp_cut_1false);
  115.                             imagesavealpha($tmp_cut_1true);
  116.                         }
  117.                         imagecopyresampled($tmp_cut_1$src00$move_x$move_y$width$height$width$height);
  118.                         $tmp imagecreatetruecolor($w$h);
  119.                         if($this->type['type'] == 'image/png') {
  120.                             //allow transparency for pngs
  121.                             imagealphablending($tmpfalse);
  122.                             imagesavealpha($tmptrue);
  123.                         }
  124.                         imagecopyresampled($tmp$tmp_cut_10000$w$h$crop_w$crop_h);
  125.                     };
  126.                     break;
  127.             }
  128.             if($this->type['type'] == 'image/png') {
  129.                 //allow transparency for pngs
  130.                 imagealphablending($tmpfalse);
  131.                 imagesavealpha($tmptrue);
  132.             }
  133.             ob_start();
  134.             switch($this->type['type']) {
  135.                 case 'image/jpeg':
  136.                     imagejpeg($tmpnull$compression);
  137.                     break;
  138.                 case 'image/png':
  139.                     imagepng($tmpnull, ($compression/10));
  140.                     break;
  141.             }
  142.             $resized ob_get_contents();
  143.             ob_end_clean();
  144.             //destroy image resources
  145.             imagedestroy($tmp);
  146.             imagedestroy($src);
  147.             return $resized;
  148.         } else {
  149.             trigger_error('IMAGE:: obsah je prázdný'E_USER_ERROR);
  150.         }
  151.     }
  152.     private function getFileName() {
  153.         $new_file_name substr(md5(rand(1000,9999).rand(1000,9999)),0,10);
  154.         $lock_file_path $this->base_dir.'/ds/lock/'.$new_file_name.'.lock';
  155.         if(file_exists($lock_file_path)) {
  156.             return $this->getFileName();
  157.         } else {
  158.             fclose(fopen($lock_file_path"w"));
  159.             return $new_file_name;
  160.         }
  161.     }
  162.     public function getOrigImage($imageid,$image_folder 'orig') {
  163.         if($this->type) {
  164.             $file_path $this->base_dir.'/ds/'.$image_folder.'/'.$imageid.'.'.$this->type['extension'];
  165.             $content file_get_contents($file_path);
  166.             if(file_exists($file_path)) {
  167.                 return array(
  168.                     'header_type' => $this->type['type'],
  169.                     'image' => $content,
  170.                     'path' => $file_path
  171.                 );
  172.             } else {
  173.                 return false;
  174.             }
  175.         } else {
  176.             trigger_error('IMAGE:: neznamy typ obrazku'E_USER_ERROR);
  177.         }
  178.     }
  179.     public function getOrigImageWatermark($imageid,$image_folder 'orig') {
  180.         if($this->type) {
  181.             $file_path $this->base_dir.'/ds/'.$image_folder.'/'.$imageid.'.'.$this->type['extension'];
  182.             $im imagecreatefromjpeg($file_path);
  183.             $stamp imagecreatefrompng(dirname(__DIR__).'/Resources/mask/vodoznak.png');
  184.             $x imagesx($im); $xC $x/2;
  185.             $y imagesy($im); $yC $y/2;
  186.             $sx imagesx($stamp); $sxC $sx/2;
  187.             $sy imagesy($stamp); $sxC $sy/2;
  188.             if($sx >= $x) {
  189.                 $sxDelta = ($x-$sx)/2;
  190.             } else {
  191.                 $sxDelta = ($sx-$x)/2;
  192.             }
  193.             if($sy >= $y) {
  194.                 $syDelta = ($y-$sy)/2;
  195.             } else {
  196.                 $syDelta = ($sy-$y)/2;
  197.             }
  198.             $marge_right 10;
  199.             $marge_bottom 10;
  200.             $sx imagesx($stamp);
  201.             $sy imagesy($stamp);
  202.             imagecopy($im$stamp$sxDelta$syDelta00imagesx($stamp), imagesy($stamp));
  203. /*
  204.             if($_SERVER['REMOTE_ADDR'] == '88.101.251.206') {
  205.                 imagecopy($im, $stamp, $sxDelta, $syDelta, 0, 0, imagesx($stamp), imagesy($stamp));
  206.             } else {
  207.                 imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
  208.             }
  209. */
  210.             
  211.             ob_start();
  212.             imagejpeg($imnull90);
  213.             $content ob_get_contents();
  214.             ob_end_clean();
  215.             if(file_exists($file_path)) {
  216.                 return array(
  217.                     'header_type' => $this->type['type'],
  218.                     'image' => $content,
  219.                     'path' => $file_path
  220.                 );
  221.             } else {
  222.                 return false;
  223.             }
  224.         } else {
  225.             trigger_error('IMAGE:: neznamy typ obrazku'E_USER_ERROR);
  226.         }
  227.     }
  228.     public function getThImage($imageid){
  229.         return $this->getOrigImage($imageid,'th');
  230.     }
  231.     public function getCutImage($imageid,$w 0,$h 0,$method 'exact') {
  232.         if($imageid != '') {
  233.             $file_path $this->base_dir.'/ds/custom/'.$imageid.'_'.$method.'_'.$w.'_'.$h.'.'.$this->type['extension'];
  234.             if(file_exists($file_path)) {
  235.                 return array(
  236.                     'header_type' => $this->type['type'],
  237.                     'image' => file_get_contents($file_path)
  238.                 );
  239.             } else {
  240.                 $orig_image $this->getOrigImage($imageid);
  241.                 $this->mime $orig_image['image'];
  242.                 $imagedata $this->getResize($w,$h,$method);
  243.                 file_put_contents($file_path,$imagedata);
  244.                 return array(
  245.                     'header_type' => $this->type['type'],
  246.                     'image' => $imagedata
  247.                 );
  248.             }
  249.         } else {
  250.             trigger_error('IMAGE:: neznamy obrazek'E_USER_ERROR);
  251.         }
  252.     }
  253.     public function getMissingImage($image_name '') {
  254.         $im imagecreatetruecolor(300300);
  255.         $text_color imagecolorallocate($im2331491);
  256.         imagestring($im1020130,  'Image '.$image_name.' is missing'$text_color);
  257.         ob_start();
  258.         imagejpeg($imNULL85);
  259.         $contents ob_get_contents();
  260.         ob_end_clean();
  261.         imagedestroy($im);
  262.         return array(
  263.             'header_type' => 'image/jpeg',
  264.             'image' => $contents
  265.         );
  266.     }
  267.     public function clearImageCache($imageid null) {
  268.         $d dir($this->base_dir.'/ds/custom/');
  269.         while(false !== ($entry $d->read())) {
  270.             if($entry != '.' && $entry != '..') {
  271.                 $pass_erase true;
  272.                 if($imageid) {
  273.                     $pass_erase false;
  274.                     $a_name explode('_',$entry);
  275.                     if(isset($a_name[0])) {
  276.                         if($a_name[0] == $imageid)
  277.                             $pass_erase true;
  278.                     }
  279.                 }
  280.                 if($pass_erase) {
  281.                     unlink($this->base_dir.'/ds/custom/'.$entry);
  282.                 }
  283.             }
  284.         }
  285.         $d->close();
  286.     }
  287. }