<?php
namespace AD13\ImageBundle\Service;
class Image {
var $mime = false;
var $type = false;
var $base_dir = '';
public function __construct() {
$this->base_dir = getcwd();
$this->doConfigTest();
}
private function doConfigTest() {
$test_dirs = array(
'ds',
'ds/custom',
'ds/lock',
'ds/orig',
'ds/th'
);
foreach($test_dirs as $dir) {
$dir_path = $this->base_dir.'/'.$dir;
if(!is_dir($dir_path)) {
if(!mkdir($dir_path)) {
trigger_error('IMAGE:: '.$dir_path.' se nepodarilo vytvorit', E_USER_ERROR);
}
}
}
}
public function setMime($mime) {
$this->mime = $mime;
}
public function setType($type) {
switch($type) {
default:
trigger_error('IMAGE:: neznamy typ obrazku: '.$type, E_USER_ERROR);
break;
case 'JPG':
case 'jpg':
case 'jpeg':
$this->type = array('extension' => 'jpg', 'type' => 'image/jpeg', 'image_create_func' => 'imagecreatefromjpeg', 'image_save_func' => 'imagejpeg');
break;
case 'png':
$this->type = array('extension' => 'png', 'type' => 'image/png', 'image_create_func' => 'imagecreatefrompng', 'image_save_func' => 'imagepng');
break;
}
}
public function setTypeByName($file_name) {
$fn_expl = explode('.',$file_name);
$ext = $fn_expl[count($fn_expl)-1];
$this->setType($ext);
}
public function persist() {
if($this->mime) {
$new_file_name = $this->getFileName();
$orig_img = imagecreatefromstring($this->mime);
list($width, $height) = array(imagesx($orig_img), imagesy($orig_img));
//save original
if($width > 3840) {
file_put_contents($this->base_dir.'/ds/orig/'.$new_file_name.'.'.$this->type['extension'] , $this->getResize(3840,0));
} else {
file_put_contents($this->base_dir.'/ds/orig/'.$new_file_name.'.'.$this->type['extension'] , $this->mime);
}
//save thumbnail
file_put_contents($this->base_dir.'/ds/th/'.$new_file_name.'.'.$this->type['extension'] , $this->getResize(300,0));
return array(
'new_file_name' => $new_file_name,
'extension' => $this->type['extension'],
'width' => $width,
'height' => $height
);
} else {
trigger_error('IMAGE:: obsah je prázdný', E_USER_ERROR);
}
}
public function getResize($w = 0,$h = 0,$method = 'exact', $compression = 95) {
if($this->mime) {
$src = imagecreatefromstring($this->mime);
//get image size from $src handle
list($width, $height) = array(imagesx($src), imagesy($src));
switch($method) {
case 'exact':
$aspect = ($width/$height);
if($w == 0)
$w = ($h*$aspect);
if($h == 0)
$h = ($w/$aspect);
$tmp = imagecreatetruecolor($w, $h);
if($this->type['type'] == 'image/png') {
//allow transparency for pngs
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
}
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $w, $h, $width, $height);
break;
case 'crop-best':
$new_aspect = ($w/$h);
if($w < $width or $h < $height) {
$crop_w = 0;
$crop_h = 0;
$end_found = false;
while(!$end_found) {
$crop_w++;
$crop_h = ($crop_w/$new_aspect);
if($crop_w >= $width)
$end_found = true;
if($crop_h >= $height)
$end_found = true;
}
//posuny
$move_x = ($width-$crop_w)/2;
$move_y = ($height-$crop_h)/2;
$tmp_cut_1 = imagecreatetruecolor($crop_w, $crop_h);
if($this->type['type'] == 'image/png') {
//allow transparency for pngs
imagealphablending($tmp_cut_1, false);
imagesavealpha($tmp_cut_1, true);
}
imagecopyresampled($tmp_cut_1, $src, 0, 0, $move_x, $move_y, $width, $height, $width, $height);
$tmp = imagecreatetruecolor($w, $h);
if($this->type['type'] == 'image/png') {
//allow transparency for pngs
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
}
imagecopyresampled($tmp, $tmp_cut_1, 0, 0, 0, 0, $w, $h, $crop_w, $crop_h);
};
break;
}
if($this->type['type'] == 'image/png') {
//allow transparency for pngs
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
}
ob_start();
switch($this->type['type']) {
case 'image/jpeg':
imagejpeg($tmp, null, $compression);
break;
case 'image/png':
imagepng($tmp, null, ($compression/10));
break;
}
$resized = ob_get_contents();
ob_end_clean();
//destroy image resources
imagedestroy($tmp);
imagedestroy($src);
return $resized;
} else {
trigger_error('IMAGE:: obsah je prázdný', E_USER_ERROR);
}
}
private function getFileName() {
$new_file_name = substr(md5(rand(1000,9999).rand(1000,9999)),0,10);
$lock_file_path = $this->base_dir.'/ds/lock/'.$new_file_name.'.lock';
if(file_exists($lock_file_path)) {
return $this->getFileName();
} else {
fclose(fopen($lock_file_path, "w"));
return $new_file_name;
}
}
public function getOrigImage($imageid,$image_folder = 'orig') {
if($this->type) {
$file_path = $this->base_dir.'/ds/'.$image_folder.'/'.$imageid.'.'.$this->type['extension'];
$content = file_get_contents($file_path);
if(file_exists($file_path)) {
return array(
'header_type' => $this->type['type'],
'image' => $content,
'path' => $file_path
);
} else {
return false;
}
} else {
trigger_error('IMAGE:: neznamy typ obrazku', E_USER_ERROR);
}
}
public function getOrigImageWatermark($imageid,$image_folder = 'orig') {
if($this->type) {
$file_path = $this->base_dir.'/ds/'.$image_folder.'/'.$imageid.'.'.$this->type['extension'];
$im = imagecreatefromjpeg($file_path);
$stamp = imagecreatefrompng(dirname(__DIR__).'/Resources/mask/vodoznak.png');
$x = imagesx($im); $xC = $x/2;
$y = imagesy($im); $yC = $y/2;
$sx = imagesx($stamp); $sxC = $sx/2;
$sy = imagesy($stamp); $sxC = $sy/2;
if($sx >= $x) {
$sxDelta = ($x-$sx)/2;
} else {
$sxDelta = ($sx-$x)/2;
}
if($sy >= $y) {
$syDelta = ($y-$sy)/2;
} else {
$syDelta = ($sy-$y)/2;
}
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, $sxDelta, $syDelta, 0, 0, imagesx($stamp), imagesy($stamp));
/*
if($_SERVER['REMOTE_ADDR'] == '88.101.251.206') {
imagecopy($im, $stamp, $sxDelta, $syDelta, 0, 0, imagesx($stamp), imagesy($stamp));
} else {
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
}
*/
ob_start();
imagejpeg($im, null, 90);
$content = ob_get_contents();
ob_end_clean();
if(file_exists($file_path)) {
return array(
'header_type' => $this->type['type'],
'image' => $content,
'path' => $file_path
);
} else {
return false;
}
} else {
trigger_error('IMAGE:: neznamy typ obrazku', E_USER_ERROR);
}
}
public function getThImage($imageid){
return $this->getOrigImage($imageid,'th');
}
public function getCutImage($imageid,$w = 0,$h = 0,$method = 'exact') {
if($imageid != '') {
$file_path = $this->base_dir.'/ds/custom/'.$imageid.'_'.$method.'_'.$w.'_'.$h.'.'.$this->type['extension'];
if(file_exists($file_path)) {
return array(
'header_type' => $this->type['type'],
'image' => file_get_contents($file_path)
);
} else {
$orig_image = $this->getOrigImage($imageid);
$this->mime = $orig_image['image'];
$imagedata = $this->getResize($w,$h,$method);
file_put_contents($file_path,$imagedata);
return array(
'header_type' => $this->type['type'],
'image' => $imagedata
);
}
} else {
trigger_error('IMAGE:: neznamy obrazek', E_USER_ERROR);
}
}
public function getMissingImage($image_name = '') {
$im = imagecreatetruecolor(300, 300);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 10, 20, 130, 'Image '.$image_name.' is missing', $text_color);
ob_start();
imagejpeg($im, NULL, 85);
$contents = ob_get_contents();
ob_end_clean();
imagedestroy($im);
return array(
'header_type' => 'image/jpeg',
'image' => $contents
);
}
public function clearImageCache($imageid = null) {
$d = dir($this->base_dir.'/ds/custom/');
while(false !== ($entry = $d->read())) {
if($entry != '.' && $entry != '..') {
$pass_erase = true;
if($imageid) {
$pass_erase = false;
$a_name = explode('_',$entry);
if(isset($a_name[0])) {
if($a_name[0] == $imageid)
$pass_erase = true;
}
}
if($pass_erase) {
unlink($this->base_dir.'/ds/custom/'.$entry);
}
}
}
$d->close();
}
}