info = getimagesize($options['path']); if($this->info && !empty($this->info['mime'])){ $this->info['width']=$this->info[0]; $this->info['height']=$this->info[1]; unset($this->info[0]); unset($this->info[1]); unset($this->info[2]); unset($this->info[3]); //creating an empty transparent canvas if($this->info['mime']=='image/jpg' || $this->info['mime']=='image/jpeg' || $this->info['mime']=='image/pjpeg'){ $this->gd = imagecreatefromjpeg($options['path']); } elseif($this->info['mime']=='image/gif'){ $this->gd = imagecreatefromgif($options['path']); } elseif($this->info['mime']=='image/png'){ $this->gd = imagecreatefrompng($options['path']); } } else{ throw new Zend_Exception('File is not an image!'); } } elseif(!isset($options['path'])){ $this->gd = $this->createNewGdResource($options); } else{ throw new Zend_Exception('Incorrect parameter passed or no file found!'); } return $this; } public function createNewGdResource(array $options){ if(isset($options['width']) && $options['width']>0 && isset($options['height']) && $options['height']>0){ $newResource = imagecreatetruecolor($options['width'], $options['height']); $this->info['width'] = $options['width']; $this->info['height'] = $options['height']; if(!isset($options['color'])){ imagealphablending($newResource,false); imagesavealpha($newResource,true); $color = imagecolorallocatealpha($newResource,255,255,255,127); imagefilledrectangle($newResource,0,0,$this->info['width'],$this->info['height'],$color); }else{ $c = $this->calculateHex($options['color']); imagefilledrectangle($newResource, 0, 0, $this->info['width'], $this->info['height'], imagecolorallocate($newResource, $c['red'], $c['green'], $c['blue'])); } return $newResource; } else{ throw new Zend_Exception('Image width and height must be positive numbers!'); } } public function adjustAlpha($alpha){ $tmp_image = $this->createNewGdResource(array('width'=>$this->info['width'], 'height' => $this->info['height'])); for ($y=0;$y<$this->info['height'];$y++){ for ($x=0;$x<$this->info['width'];$x++){ $colors=imagecolorsforindex($this->gd, @imagecolorat($this->gd, $x,$y)); $ca = 0 + $colors['alpha'] - $alpha; if($ca > 0 && $ca <=127 && $colors['alpha']<127){ imagesetpixel($tmp_image,$x,$y, imagecolorallocatealpha($tmp_image, $colors['red'], $colors['green'], $colors['blue'],$ca)); } elseif($ca <= 0 && $colors['alpha']<127){ imagesetpixel($tmp_image,$x,$y, imagecolorallocatealpha($tmp_image, $colors['red'], $colors['green'], $colors['blue'],0)); } else{ imagesetpixel($tmp_image,$x,$y, imagecolorallocatealpha($tmp_image, $colors['red'], $colors['green'], $colors['blue'],127)); } } } $this->gd = $tmp_image; return $this; } public function resize(array $options){ //sanity checks if(!isset($options['min_width']) || $options['min_width']<1){$options['min_width'] = 1;}; if(!isset($options['min_height']) || $options['min_height']<1){$options['min_height'] = 1;}; if((!isset($options['max_width']) || $options['max_width']<$options['min_width']) || (!isset($options['max_height']) || $options['max_height']<$options['min_height'])){ throw new Zend_Exception('Maximum width and height values are wrong'); }; $org_width = $this->info['width']; $org_height = $this->info['height']; //min width if ($this->info['width'] < $options['min_width']){ $this->info['height'] = ($this->info['height'] * ($options['min_width'] / $this->info['width'])); $this->info['width'] = ($this->info['width'] * ($options['min_width'] / $this->info['width'])); } //checking the height with new height from last operation if ($this->info['height']< $options['min_height']) { $this->info['width'] = ($this->info['width'] * ($options['min_height'] / $this->info['height'])); $this->info['height'] = ($this->info['height'] * ($options['min_height'] / $this->info['height'])); } if ($this->info['width'] > $options['max_width']) { $this->info['height'] = ($this->info['height'] * ($options['max_width'] / $this->info['width'])); $this->info['width'] = ($this->info['width'] * ($options['max_width'] / $this->info['width'])); } //checking the height with new height from last operation if ($this->info['height'] > $options['max_height']) { $this->info['width'] = ($this->info['width'] * ($options['max_height'] / $this->info['height'])); $this->info['height'] = ($this->info['height'] * ($options['max_height'] / $this->info['height'])); } $tmp_image = $this->createNewGdResource(array('width'=>$this->info['width'], 'height' => $this->info['height'])); imagealphablending($tmp_image,true); imagesavealpha($tmp_image,false); imagecopyresampled($tmp_image, $this->gd, 0, 0, 0, 0, $this->info['width'], $this->info['height'], $org_width, $org_height); $this->gd=$tmp_image; imagesavealpha($this->gd,true); return $this; } /** * output file to disk * * @param string $id_name * @param string $path filepath * @param string $type jpg/gif/png (optional) * @param int $compression 0-100(optional) */ public function output($type=null,$path=null,$compression=80){ //flushing out if(!$path){ switch ($type) { case 'image/png': header("Content-type: image/png"); return imagepng($this->gd); break; case 'image/gif': header("Content-type: image/gif"); return imagegif($this->gd); break; case 'image/jpg': header("Content-type: image/jpeg"); return imagejpeg($this->gd, null, $compression); break; case 'image/jpeg': header("Content-type: image/jpeg"); return imagejpeg($this->gd, null, $compression); break; default: header("Content-type: image/png"); return imagepng($this->gd); break; } } else{ switch ($type) { case 'image/png': $bool = imagepng($this->gd, $path); return $bool; break; case 'image/gif': $bool = imagegif($this->gd, $path); return $bool; break; case 'image/jpg': $bool = imagejpeg($this->gd, $path, $compression); return $bool; break; case 'image/jpeg': $bool = imagejpg($this->gd, $path, $compression); return $bool; break; default: $bool = imagepng($this->gd, $path); return $bool; break; } } } /** * watermarking * * @param string $destination_id * @param string $watermark_id * @param string $position * @param int $transparency (optional) * @param int $start_x (optional) * @param int $start_y (optional) */ public function blend($extGd, array $options){ if($extGd instanceof Reactor_Image_Adapter_Gd){ if(!isset($options['offset_x'])){$options['offset_x'] = 0;} if(!isset($options['offset_y'])){$options['offset_y'] = 0;} switch ($options['position']) { case 'top-left': $x_position=0+$options['offset_x']; $y_position=0+$options['offset_y']; break; case 'top': $x_position=(($this->info['width']-$extGd->info['width'])/2)+$options['offset_x']; $y_position=0+$options['offset_y']; break; case 'top-right': $x_position=($this->info['width']-$extGd->info['width'])+$options['offset_x']; $y_position=0+$options['offset_y']; break; case 'right': $x_position=($this->info['width']-$extGd->info['width'])+$options['offset_x']; $y_position=(($this->info['height']-$extGd->info['height'])/2)+$options['offset_y']; break; case 'bottom-right': $x_position=($this->info['width']-$extGd->info['width'])+$options['offset_x']; $y_position=($this->info['height']-$extGd->info['height'])+$options['offset_y']; break; case 'bottom': $x_position=(($this->info['width']-$extGd->info['width'])/2)+$options['offset_x']; $y_position=($this->info['height']-$extGd->info['height'])+$options['offset_y']; break; case 'bottom-left': $x_position=0+$options['offset_x']; $y_position=($this->info['height']-$extGd->info['height'])+$options['offset_y']; break; case 'left': $x_position=0+$options['offset_x']; $y_position=(($this->info['height']-$extGd->info['height'])/2)+$options['offset_y']; break; case 'left': $x_position=0+$options['offset_x']; $y_position=(($this->info['height']-$extGd->info['height'])/2)+$options['offset_y']; break; default: $x_position=(($this->info['width']-$extGd->info['width'])/2)+$options['offset_x']; $y_position=(($this->info['height']-$extGd->info['height'])/2)+$options['offset_y']; break; } imagealphablending($this->gd,true); imagecopy($this->gd,$extGd->gd, $x_position, $y_position, 0,0,$extGd->info['width'],$extGd->info['height']); return $this; }else{ throw new Zend_Exception('Expected watermark to be instance of imaging class too'); } } public function addText(array $options){ if(!isset($options['text']) || !isset($options['size']) || !isset($options['position']) || !isset($options['font']) || !file_exists($options['font'])){ throw new Zend_Exception('Essential params or fonts are missing'); } if(!isset($options['color'])){$options['color']='000000';}; if(!isset($options['alpha'])){$options['alpha']=0;}; if(!isset($options['rotate'])){$options['rotate']=0;}; if(!isset($options['alpha'])){$options['alpha']=0;}; if(!isset($options['offset_x'])){$options['offset_x']=0;}; if(!isset($options['offset_y'])){$options['offset_y']=0;}; $c = $this->calculateHex($options['color']); $text_dims = (imagettfbbox( $options['size'], 0, $options['font'], $options['text'].'+gji!YJ')); $textHeight = abs($text_dims[5]) + abs($text_dims[3]); $text_dims = (imagettfbbox( $options['size'], -$options['rotate'], $options['font'], $options['text'])); //which quater of Cartesian coordinate system are we ? if($text_dims[6]>0 && ($text_dims[7]*-1)>=0){ $text_dims['height_offset'] = ($text_dims[7]*-1) + $text_dims[3]; $text_dims['width_offset'] = $text_dims[4]; //top/bottom transformations if($options['position'] == 'bottom'){ $text_dims['height_offset'] = $text_dims['height_offset']; } elseif($options['position'] == 'top-left' || $options['position'] == 'top' || $options['position'] == 'top-right'){ $text_dims['height_offset'] = 0 + $textHeight; } elseif($options['position']=='center'){ $text_dims['height_offset'] = $text_dims['height_offset'] - $textHeight*2; } if($options['position'] == 'top-left'|| $options['position'] == 'left' || $options['position'] == 'bottom-left'){ $text_dims['width_offset'] = 0; } if($options['position'] == 'left' || $options['position'] == 'bottom-left'){ $text_dims['height_offset'] = $text_dims['height_offset']*-1; } } elseif($text_dims[6]>=0 && ($text_dims[7]*-1)<0){ $text_dims['height_offset'] = $text_dims[5] ; $text_dims['width_offset'] = abs($text_dims[2]) + $text_dims[6]; //top/bottom transformations if($options['position'] == 'bottom-left' || $options['position'] == 'bottom' || $options['position'] == 'bottom-right'){ $text_dims['height_offset'] = $text_dims['height_offset']; } elseif($options['position'] == 'top'){ $text_dims['height_offset'] = 0 - $textHeight; } if($options['position'] == 'left' || $options['position'] == 'bottom-left'){ $text_dims['width_offset'] = $text_dims['width_offset']; $text_dims['height_offset'] = 0 - $text_dims['height_offset'] - $textHeight; } elseif($options['position']=='top' || $options['position']=='center' || $options['position']=='bottom'){ $text_dims['width_offset'] = $text_dims['width_offset']*-1; } elseif($options['position'] == 'top-left'){ $text_dims['height_offset'] = 0 ; } if($options['position'] == 'top-right' || $options['position'] == 'right' || $options['position'] == 'bottom-right'){ $text_dims['width_offset'] = $text_dims['width_offset']= 0 + $textHeight; } if($options['position'] == 'top-right'){ $text_dims['height_offset']= 0; } } elseif($text_dims[6]<0 && ($text_dims[7]*-1)<=0){ $text_dims['height_offset'] = $text_dims[3] + $text_dims[7]; $text_dims['width_offset'] = $text_dims[4]; //top/bottom transformations if($options['position'] == 'bottom-left' || $options['position'] == 'bottom' || $options['position'] == 'bottom-right'){ $text_dims['height_offset'] = 0 + $textHeight; } elseif($options['position'] == 'top-left' || $options['position'] == 'top' || $options['position'] == 'top-right'){ $text_dims['height_offset'] = $text_dims['height_offset']*-1; } if($options['position'] == 'top-right' || $options['position'] == 'right' || $options['position'] == 'bottom-right'){ $text_dims['width_offset'] = 0; } } else{ $text_dims['height_offset'] = $text_dims[3]; $text_dims['width_offset'] = ($text_dims[6]*-1) + $text_dims[4]; //top/bottom transformations if($options['position'] == 'bottom-left' || $options['position'] == 'bottom' || $options['position'] == 'bottom-right'){ $text_dims['height_offset'] = 0 + $textHeight/2; } elseif($options['position']=='left' || $options['position']=='center' || $options['position']=='right'){ $text_dims['height_offset'] = $text_dims['height_offset'] - $textHeight; } elseif($options['position']=='top'){ $text_dims['height_offset'] = $text_dims['height_offset']*-1; } if($options['position'] == 'top-left'|| $options['position'] == 'left' || $options['position'] == 'bottom-left'){ $text_dims['width_offset'] = $text_dims[6]*-1; $text_dims['height_offset'] = $text_dims['height_offset']*-1 + $textHeight/2; } if($options['position'] == 'top-right'){ $text_dims['height_offset']= $text_dims['height_offset']*-1; } } switch ($options['position']) { case 'top-left': $x_position= 0 + $text_dims['width_offset'] + $options['offset_x']; $y_position= 0 + $text_dims['height_offset'] + $options['offset_y']; break; case 'top': $x_position = $this->info['width']/2 - $text_dims['width_offset']/2 + $options['offset_x']; $y_position = $textHeight + $text_dims['height_offset'] + $options['offset_y']; break; case 'top-right': $x_position=($this->info['width']- $text_dims['width_offset'])+$options['offset_x']; $y_position = $textHeight + $text_dims['height_offset'] + $options['offset_y']; break; case 'right': $x_position= $this->info['width'] - $text_dims['width_offset'] + $options['offset_x']; $y_position= $this->info['height']/2 - $text_dims['height_offset']/2 + $options['offset_y']; break; case 'bottom-right': $x_position= $this->info['width'] - $text_dims['width_offset'] + $options['offset_x']; $y_position= $this->info['height'] - $text_dims['height_offset'] + $options['offset_y']; break; case 'bottom': $x_position= $this->info['width']/2 - $text_dims['width_offset']/2 + $options['offset_x']; $y_position= $this->info['height'] - $text_dims['height_offset'] + $options['offset_y']; break; case 'bottom-left': $x_position= 0 + $text_dims['width_offset'] + $options['offset_x']; $y_position= $this->info['height'] + $text_dims['height_offset'] + $options['offset_y']; break; case 'left': $x_position = 0 + $text_dims['width_offset'] + $options['offset_x']; $y_position= $this->info['height']/2 + $text_dims['height_offset']/2 + $options['offset_y']; break; default: $x_position= $this->info['width']/2 - $text_dims['width_offset']/2 + $options['offset_x']; $y_position= $this->info['height']/2 - $text_dims['height_offset']/2 + $options['offset_y']; break; } if(!empty($this->gd)){ imagettftext($this->gd, $options['size'], -$options['rotate'], $x_position, $y_position, imagecolorallocatealpha($this->gd, $c['red'], $c['green'], $c['blue'],$options['alpha']), $options['font'], $options['text']); } return $this; } /** * draw elipse * * @param string $id_name * @param int $startX * @param int $startY * @param int $width * @param int $height * @param bool $filled (optional) * @param int $color (optional) * @param int $alpha (optional) */ public function drawElipse(array $options){ if(!isset($options['start_x']) || !isset($options['start_y']) || !isset($options['width']) || !isset($options['height'])){ throw new Zend_Exception('Essential params are missing'); } if(!isset($options['filled'])){$options['filled']=true;}; if(!isset($options['color'])){$options['color']='000000';}; if(!isset($options['alpha'])){$options['alpha']=127;}; $c = $this->calculateHex($options['color']); $options['alpha']=127-$options['alpha']; if($options['filled']==true){ if(!$this->styled){ imagefilledellipse($this->gd, $options['start_x'], $options['start_y'], $options['width'], $options['height'], imagecolorallocatealpha($this->gd, $c['red'],$c['green'],$c['blue'],$options['alpha'])); } else{ imagefilledellipse($this->gd, $options['start_x'], $options['start_y'], $options['width'], $options['height'], IMG_COLOR_TILED); } } else{ if(!$this->styled){ imageellipse($this->gd, $options['start_x'], $options['start_y'], $options['width'], $options['height'], imagecolorallocatealpha($this->gd, $c['red'],$c['green'],$c['blue'],$options['alpha'])); } else{ imageellipse($this->gd, $options['start_x'], $options['start_y'], $options['width'], $options['height'], IMG_COLOR_TILED); } } return $this; } /** * draw eclipse arc * * @param string $id_name * @param int $centerX * @param int $centerY * @param int $width * @param int $height * @param int $start * @param int $end * @param bool $filled * @param hex $color * @param int $alpha */ public function drawArc(array $options){ if(!isset($options['start_x']) || !isset($options['start_y']) || !isset($options['width']) || !isset($options['height']) || !isset($options['start_cutout']) || !isset($options['end_cutout'])){ throw new Zend_Exception('Essential params are missing'); } if(!isset($options['filled'])){$options['filled']=true;}; if(!isset($options['color'])){$options['color']='000000';}; if(!isset($options['alpha'])){$options['alpha']=127;}; $c = $this->calculateHex($options['color']); $options['alpha']=127-$options['alpha']; if($options['filled']==true){ if(!$this->styled){ imagefilledarc($this->gd, $options['start_x'], $options['start_y'], $options['width'], $options['height'],$options['start_cutout'],$options['end_cutout'], imagecolorallocatealpha($this->gd, $c['red'], $c['green'], $c['blue'],$options['alpha']),0); } else{ imagefilledarc($this->gd, $options['start_x'], $options['start_y'], $options['width'], $options['height'],$options['start_cutout'],$options['end_cutout'], IMG_COLOR_TILED,0); } } else{ if(!$this->styled){ imagefilledarc($this->gd, $options['start_x'], $options['start_y'], $options['width'], $options['height'],$options['start_cutout'],$options['end_cutout'], imagecolorallocatealpha($this->gd, $c['red'], $c['green'], $c['blue'],$options['alpha']),6); } else{ imagefilledarc($this->gd, $options['start_x'], $centerY, $options['width'], $options['height'],$options['start_cutout'],$options['end_cutout'], IMG_COLOR_TILED,6); } } return $this; } /** * rotating an image * * @param string $id_name * @param float $degrees */ public function rotate($degrees){ if(function_exists("imagerotate")) { $tmp_image_size=sqrt(pow($this->info['width'],2)+pow($this->info['height'],2)); $this->gd = imagerotate($this->gd, -$degrees,imagecolorallocate($this->gd, 0, 0, 0)); imagecolortransparent($this->gd,imagecolorallocate($this->gd, 0, 0, 0)); $this->info['width']=imagesx($this->gd); $this->info['height']=imagesy($this->gd); return $this; } else{ throw new Zend_Exception('This function is only available if PHP is compiled with the bundled version of the GD library.'); } } /** * draw line * * @param string $id_name * @param int $startX * @param int $startY * @param int $endX * @param int $endY * @param int $thickness * @param string $color * @param int $alpha */ public function drawLine(array $options){ if(!isset($options['start_x']) || !isset($options['start_y']) || !isset($options['end_x']) || !isset($options['end_y'])){ throw new Zend_Exception('Essential params are missing'); } if(!isset($options['thickness'])){$options['thickness']=1;}; if(!isset($options['filled'])){$options['filled']=true;}; if(!isset($options['color'])){$options['color']='000000';}; if(!isset($options['alpha'])){$options['alpha']=127;}; $c = $this->calculateHex($options['color']); $options['alpha']=127-$options['alpha']; $th = $options['thickness'] / 2 - 0.5; if($options['thickness']==1){ imageline($this->gd,$options['start_x'],$options['start_y'],$options['end_x'],$options['end_y'],imagecolorallocatealpha($this->gd, $c['red'], $c['green'], $c['blue'],$options['alpha'])); } elseif($options['end_x']==$options['start_x'] || $options['end_y']==$options['start_y']){ if($options['filled']==true){ imagefilledrectangle($this->gd, round(min($options['start_x'], $options['end_x']) - $th), round(min($options['start_y'], $options['end_y']) - $th), round(max($options['start_x'], $options['end_x']) + $th), round(max($options['start_y'], $options['end_y']) + $th),imagecolorallocatealpha($this->gd, $c['red'], $c['green'], $c['blue'],$options['alpha'])); } else{ imagerectangle($this->gd, round(min($options['start_x'], $options['end_x']) - $th), round(min($options['start_y'], $options['end_y']) - $th), round(max($options['start_x'], $options['end_x']) + $th), round(max($options['start_y'], $options['end_y']) + $th),imagecolorallocatealpha($this->gd, $c['red'], $c['green'], $c['blue'],$options['alpha'])); } } else{ $k = ($options['end_y'] - $options['start_y']) / ($options['end_x'] - $options['start_x']); //y = kx + q $a = $th / sqrt(1 + pow($k, 2)); $options['points'] = array( array(round($options['start_x'] - (1+$k)*$a), round($options['start_y'] + (1-$k)*$a)), array(round($options['start_x'] - (1-$k)*$a), round($options['start_y'] - (1+$k)*$a)), array(round($options['end_x'] + (1+$k)*$a), round($options['end_y'] - (1-$k)*$a)), array(round($options['end_x'] + (1-$k)*$a), round($options['end_y'] + (1+$k)*$a)) ); $options['_alphaCalculated]']= true; $this->drawPolygon($options); } return $this; } public function drawPolygon(array $options){ if(!isset($options['points'])){ throw new Zend_Exception('Essential params are missing'); } if(!isset($options['thickness'])){$options['thickness']=1;}; if(!isset($options['filled'])){$options['filled']=true;}; if(!isset($options['color'])){$options['color']='000000';}; if(!isset($options['alpha'])){$options['alpha']=127;}; $points = array(); foreach ($options['points'] as $point){ array_push($points,$point[0],$point[1]); } $c = $this->calculateHex($options['color']); if(!isset($options['_alphaCalculated]'])){ $options['alpha']=127-$options['alpha']; } if($options['filled']==true){ if(!$this->styled){ imagefilledpolygon($this->gd, $points, count($points)/2, imagecolorallocatealpha($this->gd, $c['red'], $c['green'], $c['blue'],$options['alpha'])); } else{ imagefilledpolygon($this->gd, $points, count($points)/2, IMG_COLOR_TILED); } } else{ if($this->styled){ imagepolygon($this->gd, $options['points'], count($options['points']), imagecolorallocatealpha($this->gd, $c['red'], $c['green'], $c['blue'],$options['alpha'])); } else{ imagepolygon($this->gd, $options['points'], count($options['points']), IMG_COLOR_BRUSHED); } } } public function setGreyscale(){ if(function_exists("imagefilter")) { imagefilter($this->gd, IMG_FILTER_GRAYSCALE); return $this; } else{ throw new Zend_Exception('This function is only available if PHP is compiled with the bundled version of the GD library.'); } } public function setNegative(){ if(function_exists("imagefilter")) { imagefilter($this->gd, IMG_FILTER_NEGATE); return $this; } else{ throw new Zend_Exception('This function is only available if PHP is compiled with the bundled version of the GD library.'); } } public function setEmboss(){ if(function_exists("imagefilter")) { imagefilter($this->gd, IMG_FILTER_EMBOSS); return $this; } else{ throw new Zend_Exception('This function is only available if PHP is compiled with the bundled version of the GD library.'); } } public function setGaussianBlur(){ if(function_exists("imagefilter")) { imagefilter($this->gd, IMG_FILTER_GAUSSIAN_BLUR); return $this; } else{ throw new Zend_Exception('This function is only available if PHP is compiled with the bundled version of the GD library.'); } } public function setSketchy(){ if(function_exists("imagefilter")) { imagefilter($this->gd, IMG_FILTER_MEAN_REMOVAL); return $this; } else{ throw new Zend_Exception('This function is only available if PHP is compiled with the bundled version of the GD library.'); } } public function setBrightness($brightness){ if(function_exists("imagefilter")) { imagefilter($this->gd, IMG_FILTER_BRIGHTNESS,$brightness); return $this; } else{ throw new Zend_Exception('This function is only available if PHP is compiled with the bundled version of the GD library.'); } } public function setContrast($contrast){ if(function_exists("imagefilter")) { imagefilter($this->gd, IMG_FILTER_CONTRAST,$contrast); return $this; } else{ throw new Zend_Exception('This function is only available if PHP is compiled with the bundled version of the GD library.'); } } public function colorize($color){ if(function_exists("imagefilter")) { $c = $this->calculateHex($color); imagefilter($this->gd, IMG_FILTER_COLORIZE,$c['red'],$c['green'],$c['blue']); return $this; } else{ throw new Zend_Exception('This function is only available if PHP is compiled with the bundled version of the GD library.'); } } } ?>