12 namespace abeautifulsite;
29 protected $image, $filename, $original_info, $width, $height, $imagestring;
45 function __construct($filename = null, $width = null, $height = null, $color = null) {
47 $this->load($filename);
49 $this->create($width, $height, $color);
60 imagedestroy($this->image);
79 return $this->thumbnail($width, $height);
91 switch ($this->original_info[
'exif'][
'Orientation']) {
145 if ($this->width <= $max_width && $this->height <= $max_height) {
150 $aspect_ratio = $this->height / $this->width;
153 if ($this->width > $max_width) {
155 $height = $width * $aspect_ratio;
157 $width = $this->width;
158 $height = $this->height;
162 if ($height > $max_height) {
163 $height = $max_height;
164 $width = $height / $aspect_ratio;
167 return $this->resize($width, $height);
180 function blur($type =
'selective', $passes = 1) {
181 switch (strtolower($type)) {
183 $type = IMG_FILTER_GAUSSIAN_BLUR;
186 $type = IMG_FILTER_SELECTIVE_BLUR;
189 for ($i = 0; $i < $passes; $i++) {
190 imagefilter($this->image, $type);
204 imagefilter($this->image, IMG_FILTER_BRIGHTNESS, $this->keep_within($level, -255, 255));
218 imagefilter($this->image, IMG_FILTER_CONTRAST, $this->keep_within($level, -100, 100));
233 $rgba = $this->normalize_color($color);
234 $alpha = $this->keep_within(127 - (127 * $opacity), 0, 127);
235 imagefilter($this->image, IMG_FILTER_COLORIZE, $this->keep_within($rgba[
'r'], 0, 255), $this->keep_within($rgba[
'g'], 0, 255), $this->keep_within($rgba[
'b'], 0, 255), $alpha);
250 function create($width, $height = null, $color = null) {
252 $height = $height ?: $width;
253 $this->width = $width;
254 $this->height = $height;
255 $this->image = imagecreatetruecolor($width, $height);
256 $this->original_info = array(
259 'orientation' => $this->get_orientation(),
262 'mime' =>
'image/png'
284 function crop($x1, $y1, $x2, $y2) {
288 list($x1, $x2) = array($x2, $x1);
291 list($y1, $y2) = array($y2, $y1);
293 $crop_width = $x2 - $x1;
294 $crop_height = $y2 - $y1;
297 $new = imagecreatetruecolor($crop_width, $crop_height);
298 imagealphablending($new,
false);
299 imagesavealpha($new,
true);
300 imagecopyresampled($new, $this->image, 0, 0, $x1, $y1, $crop_width, $crop_height, $crop_width, $crop_height);
303 $this->width = $crop_width;
304 $this->height = $crop_height;
318 imagefilter($this->image, IMG_FILTER_GRAYSCALE);
329 imagefilter($this->image, IMG_FILTER_EDGEDETECT);
340 imagefilter($this->image, IMG_FILTER_EMBOSS);
353 function fill($color =
'#000000') {
355 $rgba = $this->normalize_color($color);
356 $fill_color = imagecolorallocatealpha($this->image, $rgba[
'r'], $rgba[
'g'], $rgba[
'b'], $rgba[
'a']);
357 imagealphablending($this->image,
false);
358 imagesavealpha($this->image,
true);
359 imagefilledrectangle($this->image, 0, 0, $this->width, $this->height, $fill_color);
375 $aspect_ratio = $this->height / $this->width;
376 $width = $height / $aspect_ratio;
378 return $this->resize($width, $height);
392 $aspect_ratio = $this->height / $this->width;
393 $height = $width * $aspect_ratio;
395 return $this->resize($width, $height);
409 $new = imagecreatetruecolor($this->width, $this->height);
410 imagealphablending($new,
false);
411 imagesavealpha($new,
true);
413 switch (strtolower($direction)) {
415 for ($y = 0; $y < $this->height; $y++) {
416 imagecopy($new, $this->image, 0, $y, 0, $this->height - $y - 1, $this->width, 1);
420 for ($x = 0; $x < $this->width; $x++) {
421 imagecopy($new, $this->image, $x, 0, $this->width - $x - 1, 0, 1, $this->height);
439 return $this->height;
450 if (imagesx($this->image) > imagesy($this->image)) {
454 if (imagesx($this->image) < imagesy($this->image)) {
476 return $this->original_info;
496 imagefilter($this->image, IMG_FILTER_NEGATE);
512 if (!extension_loaded(
'gd')) {
513 throw new Exception(
'Required extension GD is not loaded.');
515 $this->filename = $filename;
516 return $this->get_meta_data();
528 if (!extension_loaded(
'gd')) {
529 throw new Exception(
'Required extension GD is not loaded.');
532 $this->imagestring = base64_decode(str_replace(
' ',
'+',preg_replace(
'#^data:image/[^;]+;base64,#',
'', $base64string)));
533 $this->image = imagecreatefromstring($this->imagestring);
534 return $this->get_meta_data();
544 imagefilter($this->image, IMG_FILTER_MEAN_REMOVAL);
559 $opacity = $this->keep_within($opacity, 0, 1) * 100;
562 $copy = imagecreatetruecolor($this->width, $this->height);
563 imagealphablending($copy,
false);
564 imagesavealpha($copy,
true);
565 imagecopy($copy, $this->image, 0, 0, 0, 0, $this->width, $this->height);
568 $this->create($this->width, $this->height, array(0, 0, 0, 127));
571 $this->imagecopymerge_alpha($this->image, $copy, 0, 0, 0, 0, $this->width, $this->height, $opacity);
587 function output($format = null, $quality = null) {
590 $quality = $quality ?: $this->quality;
593 switch (strtolower($format)) {
595 $mimetype =
'image/gif';
599 imageinterlace($this->image,
true);
600 $mimetype =
'image/jpeg';
603 $mimetype =
'image/png';
606 $info = (empty($this->imagestring)) ? getimagesize($this->filename) : getimagesizefromstring($this->imagestring);
607 $mimetype = $info[
'mime'];
613 header(
'Content-Type: '.$mimetype);
616 imagegif($this->image);
619 imagejpeg($this->image, null, round($quality));
622 imagepng($this->image, null, round(9 * $quality / 100));
625 throw new Exception(
'Unsupported image format: '.$this->filename);
647 $quality = $quality ?: $this->quality;
650 switch (strtolower($format)) {
652 $mimetype =
'image/gif';
656 imageinterlace($this->image,
true);
657 $mimetype =
'image/jpeg';
660 $mimetype =
'image/png';
663 $info = getimagesize($this->filename);
664 $mimetype = $info[
'mime'];
673 imagegif($this->image);
676 imagejpeg($this->image, null, round($quality));
679 imagepng($this->image, null, round(9 * $quality / 100));
682 throw new Exception(
'Unsupported image format: '.$this->filename);
685 $image_data = ob_get_contents();
689 return 'data:'.$mimetype.
';base64,'.base64_encode($image_data);
707 function overlay($overlay, $position =
'center', $opacity = 1, $x_offset = 0, $y_offset = 0) {
711 $overlay =
new SimpleImage($overlay);
715 $opacity = $opacity * 100;
718 switch (strtolower($position)) {
724 $x = $this->width - $overlay->width + $x_offset;
728 $x = ($this->width / 2) - ($overlay->width / 2) + $x_offset;
733 $y = $this->height - $overlay->height + $y_offset;
736 $x = $this->width - $overlay->width + $x_offset;
737 $y = $this->height - $overlay->height + $y_offset;
740 $x = ($this->width / 2) - ($overlay->width / 2) + $x_offset;
741 $y = $this->height - $overlay->height + $y_offset;
745 $y = ($this->height / 2) - ($overlay->height / 2) + $y_offset;
748 $x = $this->width - $overlay->width + $x_offset;
749 $y = ($this->height / 2) - ($overlay->height / 2) + $y_offset;
753 $x = ($this->width / 2) - ($overlay->width / 2) + $x_offset;
754 $y = ($this->height / 2) - ($overlay->height / 2) + $y_offset;
759 $this->imagecopymerge_alpha($this->image, $overlay->image, $x, $y, 0, 0, $overlay->width, $overlay->height, $opacity);
774 imagefilter($this->image, IMG_FILTER_PIXELATE, $block_size,
true);
790 $new = imagecreatetruecolor($width, $height);
792 if( $this->original_info[
'format'] ===
'gif' ) {
794 $transparent_index = imagecolortransparent($this->image);
795 if ($transparent_index >= 0) {
796 $transparent_color = imagecolorsforindex($this->image, $transparent_index);
797 $transparent_index = imagecolorallocate($new, $transparent_color[
'red'], $transparent_color[
'green'], $transparent_color[
'blue']);
798 imagefill($new, 0, 0, $transparent_index);
799 imagecolortransparent($new, $transparent_index);
803 imagealphablending($new,
false);
804 imagesavealpha($new,
true);
808 imagecopyresampled($new, $this->image, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
811 $this->width = $width;
812 $this->height = $height;
829 function rotate($angle, $bg_color =
'#000000') {
832 $rgba = $this->normalize_color($bg_color);
833 $bg_color = imagecolorallocatealpha($this->image, $rgba[
'r'], $rgba[
'g'], $rgba[
'b'], $rgba[
'a']);
834 $new = imagerotate($this->image, -($this->keep_within($angle, -360, 360)), $bg_color);
835 imagesavealpha($new,
true);
836 imagealphablending($new,
true);
839 $this->width = imagesx($new);
840 $this->height = imagesy($new);
859 function save($filename = null, $quality = null) {
862 $quality = $quality ?: $this->quality;
863 $filename = $filename ?: $this->filename;
864 $format = $this->file_ext($filename) ?: $this->original_info[
'format'];
867 switch (strtolower($format)) {
869 $result = imagegif($this->image, $filename);
873 imageinterlace($this->image,
true);
874 $result = imagejpeg($this->image, $filename, round($quality));
877 $result = imagepng($this->image, $filename, round(9 * $quality / 100));
880 throw new Exception(
'Unsupported format');
884 throw new Exception(
'Unable to save image: ' . $filename);
898 imagefilter($this->image, IMG_FILTER_GRAYSCALE);
899 imagefilter($this->image, IMG_FILTER_COLORIZE, 100, 50, 0);
910 imagefilter($this->image, IMG_FILTER_MEAN_REMOVAL);
923 imagefilter($this->image, IMG_FILTER_SMOOTH, $this->keep_within($level, -10, 10));
942 function text($text, $font_file, $font_size = 12, $color =
'#000000', $position =
'center', $x_offset = 0, $y_offset = 0) {
948 $rgba = $this->normalize_color($color);
949 $color = imagecolorallocatealpha($this->image, $rgba[
'r'], $rgba[
'g'], $rgba[
'b'], $rgba[
'a']);
952 $box = imagettfbbox($font_size, $angle, $font_file, $text);
954 throw new Exception(
'Unable to load font: '.$font_file);
956 $box_width = abs($box[6] - $box[2]);
957 $box_height = abs($box[7] - $box[1]);
960 switch (strtolower($position)) {
963 $y = 0 + $y_offset + $box_height;
966 $x = $this->width - $box_width + $x_offset;
967 $y = 0 + $y_offset + $box_height;
970 $x = ($this->width / 2) - ($box_width / 2) + $x_offset;
971 $y = 0 + $y_offset + $box_height;
975 $y = $this->height - $box_height + $y_offset + $box_height;
978 $x = $this->width - $box_width + $x_offset;
979 $y = $this->height - $box_height + $y_offset + $box_height;
982 $x = ($this->width / 2) - ($box_width / 2) + $x_offset;
983 $y = $this->height - $box_height + $y_offset + $box_height;
987 $y = ($this->height / 2) - (($box_height / 2) - $box_height) + $y_offset;
990 $x = $this->width - $box_width + $x_offset;
991 $y = ($this->height / 2) - (($box_height / 2) - $box_height) + $y_offset;
995 $x = ($this->width / 2) - ($box_width / 2) + $x_offset;
996 $y = ($this->height / 2) - (($box_height / 2) - $box_height) + $y_offset;
1001 imagettftext($this->image, $font_size, $angle, $x, $y, $color, $font_file, $text);
1022 $height = $height ?: $width;
1025 $current_aspect_ratio = $this->height / $this->width;
1026 $new_aspect_ratio = $height / $width;
1029 if ($new_aspect_ratio > $current_aspect_ratio) {
1030 $this->fit_to_height($height);
1032 $this->fit_to_width($width);
1034 $left = floor(($this->width / 2) - ($width / 2));
1035 $top = floor(($this->height / 2) - ($height / 2));
1038 return $this->crop($left, $top, $width + $left, $height + $top);
1052 if (!preg_match(
'/\./', $filename)) {
1056 return preg_replace(
'/^.*\./',
'', $filename);
1071 if(empty($this->imagestring)) {
1072 $info = getimagesize($this->filename);
1074 switch ($info[
'mime']) {
1076 $this->image = imagecreatefromgif($this->filename);
1079 $this->image = imagecreatefromjpeg($this->filename);
1082 $this->image = imagecreatefrompng($this->filename);
1085 throw new Exception(
'Invalid image: '.$this->filename);
1088 } elseif (function_exists(
'getimagesizefromstring')) {
1089 $info = getimagesizefromstring($this->imagestring);
1091 throw new Exception(
'PHP 5.4 is required to use method getimagesizefromstring');
1094 $this->original_info = array(
1095 'width' => $info[0],
1096 'height' => $info[1],
1097 'orientation' => $this->get_orientation(),
1098 'exif' => function_exists(
'exif_read_data') && $info[
'mime'] ===
'image/jpeg' && $this->imagestring === null ? $this->exif = @exif_read_data($this->filename) : null,
1099 'format' => preg_replace(
'/^image\//',
'', $info[
'mime']),
1100 'mime' => $info[
'mime']
1102 $this->width = $info[0];
1103 $this->height = $info[1];
1105 imagesavealpha($this->image,
true);
1106 imagealphablending($this->image,
true);
1132 $w = imagesx($src_im);
1133 $h = imagesy($src_im);
1136 imagealphablending($src_im,
false);
1140 for ($x = 0; $x < $w; $x++) {
1141 for ($y = 0; $y < $h; $y++) {
1142 $alpha = (imagecolorat($src_im, $x, $y) >> 24) & 0xFF;
1143 if ($alpha < $minalpha) {
1150 for ($x = 0; $x < $w; $x++) {
1151 for ($y = 0; $y < $h; $y++) {
1153 $colorxy = imagecolorat($src_im, $x, $y);
1154 $alpha = ($colorxy >> 24) & 0xFF;
1156 if ($minalpha !== 127) {
1157 $alpha = 127 + 127 * $pct * ($alpha - 127) / (127 - $minalpha);
1159 $alpha += 127 * $pct;
1162 $alphacolorxy = imagecolorallocatealpha($src_im, ($colorxy >> 16) & 0xFF, ($colorxy >> 8) & 0xFF, $colorxy & 0xFF, $alpha);
1164 if (!imagesetpixel($src_im, $x, $y, $alphacolorxy)) {
1171 imagesavealpha($dst_im,
true);
1172 imagealphablending($dst_im,
true);
1173 imagesavealpha($src_im,
true);
1174 imagealphablending($src_im,
true);
1175 imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
1193 if ($value < $min) {
1197 if ($value > $max) {
1216 if (is_string($color)) {
1218 $color = trim($color,
'#');
1220 if (strlen($color) == 6) {
1221 list($r, $g, $b) = array(
1222 $color[0].$color[1],
1223 $color[2].$color[3],
1226 } elseif (strlen($color) == 3) {
1227 list($r, $g, $b) = array(
1228 $color[0].$color[0],
1229 $color[1].$color[1],
1242 } elseif (is_array($color) && (count($color) == 3 || count($color) == 4)) {
1244 if (isset($color[
'r'], $color[
'g'], $color[
'b'])) {
1246 'r' => $this->keep_within($color[
'r'], 0, 255),
1247 'g' => $this->keep_within($color[
'g'], 0, 255),
1248 'b' => $this->keep_within($color[
'b'], 0, 255),
1249 'a' => $this->keep_within(isset($color[
'a']) ? $color[
'a'] : 0, 0, 127)
1251 } elseif (isset($color[0], $color[1], $color[2])) {
1253 'r' => $this->keep_within($color[0], 0, 255),
1254 'g' => $this->keep_within($color[1], 0, 255),
1255 'b' => $this->keep_within($color[2], 0, 255),
1256 'a' => $this->keep_within(isset($color[3]) ? $color[3] : 0, 0, 127)
overlay($overlay, $position= 'center', $opacity=1, $x_offset=0, $y_offset=0)
keep_within($value, $min, $max)
rotate($angle, $bg_color= '#000000')
output($format=null, $quality=null)
save($filename=null, $quality=null)
blur($type= 'selective', $passes=1)
__construct($filename=null, $width=null, $height=null, $color=null)
best_fit($max_width, $max_height)
create($width, $height=null, $color=null)
colorize($color, $opacity)
text($text, $font_file, $font_size=12, $color= '#000000', $position= 'center', $x_offset=0, $y_offset=0)
adaptive_resize($width, $height=null)
imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
output_base64($format=null, $quality=null)
thumbnail($width, $height=null)
load_base64($base64string)