Exemple #1
0
 function ImageResize($srcFile, $toW, $toH, $toFile = "")
 {
     global $cfg_photo_type;
     if ($toFile == '') {
         $toFile = $srcFile;
     }
     $info = '';
     $srcInfo = GetImageSize($srcFile, $info);
     switch ($srcInfo[2]) {
         case 1:
             if (!$cfg_photo_type['gif']) {
                 return FALSE;
             }
             $im = imagecreatefromgif($srcFile);
             break;
         case 2:
             if (!$cfg_photo_type['jpeg']) {
                 return FALSE;
             }
             $im = imagecreatefromjpeg($srcFile);
             break;
         case 3:
             if (!$cfg_photo_type['png']) {
                 return FALSE;
             }
             $im = imagecreatefrompng($srcFile);
             break;
         case 6:
             if (!$cfg_photo_type['bmp']) {
                 return FALSE;
             }
             $im = imagecreatefromwbmp($srcFile);
             break;
     }
     $srcW = ImageSX($im);
     $srcH = ImageSY($im);
     if ($srcW <= $toW && $srcH <= $toH) {
         return TRUE;
     }
     $toWH = $toW / $toH;
     $srcWH = $srcW / $srcH;
     if ($toWH <= $srcWH) {
         $ftoW = $toW;
         $ftoH = $ftoW * ($srcH / $srcW);
     } else {
         $ftoH = $toH;
         $ftoW = $ftoH * ($srcW / $srcH);
     }
     if ($srcW > $toW || $srcH > $toH) {
         if (function_exists("imagecreateTRUEcolor")) {
             @($ni = imagecreateTRUEcolor($ftoW, $ftoH));
             if ($ni) {
                 imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
             } else {
                 $ni = imagecreate($ftoW, $ftoH);
                 imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
             }
         } else {
             $ni = imagecreate($ftoW, $ftoH);
             imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
         }
         switch ($srcInfo[2]) {
             case 1:
                 imagegif($ni, $toFile);
                 break;
             case 2:
                 imagejpeg($ni, $toFile, 85);
                 break;
             case 3:
                 imagepng($ni, $toFile);
                 break;
             case 6:
                 imagebmp($ni, $toFile);
                 break;
             default:
                 return FALSE;
         }
         imagedestroy($ni);
     }
     imagedestroy($im);
     return TRUE;
 }
Exemple #2
0
 /**
  * Generate a gradient
  *
  * @param int    $width
  * @param int    $height
  * @param array  $color_stops      Color stops, in format (position, unit, color)
  * @param string $direction        Gradient direction (vertical, horizontal, diagonal, radial, square, diamond)
  * @param bool   $invert           Inver gradient
  * @param array  $background_color If set, the final color of the gradient will be saved here
  *
  * @return Tinyfier_Image_Tool
  */
 public static function generate($width, $height, $color_stops, $direction = 'vertical', $invert = false, &$background_color = null)
 {
     //Crear imagen
     $image = imagecreateTRUEcolor($width, $height);
     //Calcular el número de líneas a dibujar
     $lines;
     $fill_background = false;
     switch ($direction) {
         case 'vertical':
             $lines = $height;
             break;
         case 'horizontal':
             $lines = $width;
             break;
         case 'diagonal':
             $lines = max($width, $height) * 2;
             break;
         case 'radial':
             $center_x = $width / 2;
             $center_y = $height / 2;
             $rh = $height > $width ? 1 : $width / $height;
             $rw = $width > $height ? 1 : $height / $width;
             $lines = ceil(max($width, $height) / 1.5);
             //Lo correcto sería /2, pero se aplica 1.5 para expandir el degradado y hacerlo más similar al generado por los navegadores
             $fill_background = true;
             $invert = !$invert;
             //The gradient is drawn from outside to inside
             break;
         case 'square':
         case 'rectangle':
             $direction = 'square';
             $lines = max($width, $height) / 2;
             $invert = !$invert;
             //The gradient is drawn from outside to inside
             break;
         case 'diamond':
             $rh = $height > $width ? 1 : $width / $height;
             $rw = $width > $height ? 1 : $height / $width;
             $lines = min($width, $height);
             $invert = !$invert;
             //The gradient is drawn from outside to inside
             break;
         default:
             return false;
             break;
     }
     //Ordenar paradas de color
     $colors = array();
     foreach ($color_stops as $stop) {
         list($position, $unit, $color) = $stop;
         $percentage;
         switch ($unit) {
             case 'px':
                 $percentage = 100 / $lines * $position;
                 break;
             default:
                 $percentage = $position;
                 break;
         }
         $colors[floatval($position)] = Tinyfier_CSS_Color::create($color)->to_array();
     }
     ksort($colors);
     $positions = array_keys($colors);
     if (!isset($colors[0])) {
         //Usar el primero como color de inicio
         $colors[0] = $colors[reset($positions)];
     }
     if (!isset($colors[100])) {
         //Usar el último como color final
         $colors[100] = $colors[end($positions)];
     }
     //Fill background
     $background_color = $colors[100];
     if ($fill_background) {
         list($r1, $g1, $b1) = $colors[100];
         imagefill($image, 0, 0, imagecolorallocate($image, $r1, $g1, $b1));
     }
     //Invert colors
     if ($invert) {
         $invert_colors = array();
         foreach ($colors as $key => $value) {
             $invert_colors[100 - $key] = $value;
         }
         $colors = $invert_colors;
     }
     ksort($colors);
     //Draw line by line
     $incr = 1;
     $color_change_positions = array_keys($colors);
     $end_color_progress = 0;
     //Forzar que en la primera iteración se seleccione el rango de colores
     for ($i = 0; $i < $lines; $i = $i + $incr) {
         //Escoger color
         $total_progress = 100 / $lines * $i;
         if ($total_progress >= $end_color_progress) {
             //Cambiar de rango de colores
             //Buscar color inicial a partir del progreso total
             $j = intval($total_progress);
             do {
                 $color_index = array_search($j--, $color_change_positions);
             } while ($color_index === false && $j >= 0);
             //Obtener colores inicio y final para este rango
             $start_color_progress = $color_change_positions[$color_index];
             $start_color = $colors[$start_color_progress];
             $end_color_progress = $color_change_positions[$color_index + 1];
             $end_color = $colors[$end_color_progress];
         }
         $internal_progress = ($total_progress - $start_color_progress) / ($end_color_progress - $start_color_progress);
         $r = $start_color[0] + ($end_color[0] - $start_color[0]) * $internal_progress;
         $g = $start_color[1] + ($end_color[1] - $start_color[1]) * $internal_progress;
         $b = $start_color[2] + ($end_color[2] - $start_color[2]) * $internal_progress;
         $color = imagecolorallocate($image, $r, $g, $b);
         //Dibujar línea
         switch ($direction) {
             case 'vertical':
                 //Draw from top to bottom
                 imagefilledrectangle($image, 0, $i, $width, $i + $incr, $color);
                 break;
             case 'horizontal':
                 //Draw from left to right
                 imagefilledrectangle($image, $i, 0, $i + $incr, $height, $color);
                 break;
             case 'diagonal':
                 //Draw from top-left to bottom-right
                 imagefilledpolygon($image, array($i, 0, $i + $incr, 0, 0, $i + $incr, 0, $i), 4, $color);
                 break;
             case 'square':
                 //Draw from outside to center
                 imagefilledrectangle($image, $i * $width / $height, $i * $height / $width, $width - $i * $width / $height, $height - $i * $height / $width, $color);
                 break;
             case 'radial':
                 //Draw from outside to center
                 imagefilledellipse($image, $center_x, $center_y, ($lines - $i) * $rh * 2, ($lines - $i) * $rw * 2, $color);
                 break;
             case 'diamond':
                 //Draw from outside to center
                 imagefilledpolygon($image, array($width / 2, $i * $rw - 0.5 * $height, $i * $rh - 0.5 * $width, $height / 2, $width / 2, 1.5 * $height - $i * $rw, 1.5 * $width - $i * $rh, $height / 2), 4, $color);
                 break;
         }
     }
     return new Tinyfier_Image_Tool($image);
 }
Exemple #3
0
 private function createImg()
 {
     $this->im = imagecreateTRUEcolor($this->width, $this->height);
     $bgColor = imagecolorallocate($this->im, rand(200, 255), rand(200, 255), rand(100, 255));
     imagefill($this->im, 0, 0, $bgColor);
 }
Exemple #4
0
 /**
  * Moves an image file (resizing is possible)
  *
  * @param string $source_file Path of the source file
  * @param string $target_file Path of the target file
  * @param int $resize_width Width to resize
  * @param int $resize_height Height to resize
  * @param string $target_type If $target_type is set (gif, jpg, png, bmp), result image will be saved as target type
  * @param string $thumbnail_type Thumbnail type(crop, ratio)
  * @return bool TRUE: success, FALSE: failed
  */
 public static function createImageFile($source_file, $target_file, $resize_width = 0, $resize_height = 0, $target_type = '', $thumbnail_type = 'crop')
 {
     // check params
     if (($source_file = self::exists($source_file)) === FALSE) {
         return;
     }
     $target_file = self::getRealPath($target_file);
     if (!$resize_width) {
         $resize_width = 100;
     }
     if (!$resize_height) {
         $resize_height = $resize_width;
     }
     // retrieve source image's information
     $imageInfo = getimagesize($source_file);
     if (!self::checkMemoryLoadImage($imageInfo)) {
         return FALSE;
     }
     list($width, $height, $type, $attrs) = $imageInfo;
     if ($width < 1 || $height < 1) {
         return;
     }
     switch ($type) {
         case '1':
             $type = 'gif';
             break;
         case '2':
             $type = 'jpg';
             break;
         case '3':
             $type = 'png';
             break;
         case '6':
             $type = 'bmp';
             break;
         default:
             return;
     }
     if (!$target_type) {
         $target_type = $type;
     }
     $target_type = strtolower($target_type);
     // if original image is larger than specified size to resize, calculate the ratio
     $width_per = $resize_width > 0 && $width >= $resize_width ? $resize_width / $width : 1;
     $height_per = $resize_height > 0 && $height >= $resize_height ? $resize_height / $height : 1;
     $per = NULL;
     if ($thumbnail_type == 'ratio') {
         $per = $width_per > $height_per ? $height_per : $width_per;
         $resize_width = $width * $per;
         $resize_height = $height * $per;
     } else {
         $per = $width_per < $height_per ? $height_per : $width_per;
     }
     // create temporary image with target size
     $thumb = NULL;
     if (function_exists('imagecreateTRUEcolor')) {
         $thumb = imagecreateTRUEcolor($resize_width, $resize_height);
     } else {
         if (function_exists('imagecreate')) {
             $thumb = imagecreate($resize_width, $resize_height);
         }
     }
     if (!$thumb) {
         return FALSE;
     }
     imagefilledrectangle($thumb, 0, 0, $resize_width - 1, $resize_height - 1, imagecolorallocate($thumb, 255, 255, 255));
     // create temporary image having original type
     $source = NULL;
     switch ($type) {
         case 'gif':
             if (function_exists('imagecreatefromgif')) {
                 $source = @imagecreatefromgif($source_file);
             }
             break;
         case 'jpeg':
         case 'jpg':
             if (function_exists('imagecreatefromjpeg')) {
                 $source = @imagecreatefromjpeg($source_file);
             }
             break;
         case 'png':
             if (function_exists('imagecreatefrompng')) {
                 $source = @imagecreatefrompng($source_file);
             }
             break;
         case 'wbmp':
         case 'bmp':
             if (function_exists('imagecreatefromwbmp')) {
                 $source = @imagecreatefromwbmp($source_file);
             }
             break;
     }
     if (!$source) {
         imagedestroy($thumb);
         return FALSE;
     }
     // resize original image and put it into temporary image
     $new_width = (int) ($width * $per);
     $new_height = (int) ($height * $per);
     $x = 0;
     $y = 0;
     if ($thumbnail_type == 'crop') {
         $x = (int) ($resize_width / 2 - $new_width / 2);
         $y = (int) ($resize_height / 2 - $new_height / 2);
     }
     if (function_exists('imagecopyresampled')) {
         imagecopyresampled($thumb, $source, $x, $y, 0, 0, $new_width, $new_height, $width, $height);
     } else {
         imagecopyresized($thumb, $source, $x, $y, 0, 0, $new_width, $new_height, $width, $height);
     }
     // create directory
     self::makeDir(dirname($target_file));
     // write into the file
     $output = NULL;
     switch ($target_type) {
         case 'gif':
             if (function_exists('imagegif')) {
                 $output = imagegif($thumb, $target_file);
             }
             break;
         case 'jpeg':
         case 'jpg':
             if (function_exists('imagejpeg')) {
                 $output = imagejpeg($thumb, $target_file, 100);
             }
             break;
         case 'png':
             if (function_exists('imagepng')) {
                 $output = imagepng($thumb, $target_file, 9);
             }
             break;
         case 'wbmp':
         case 'bmp':
             if (function_exists('imagewbmp')) {
                 $output = imagewbmp($thumb, $target_file, 100);
             }
             break;
     }
     imagedestroy($thumb);
     imagedestroy($source);
     if (!$output) {
         return FALSE;
     }
     @chmod($target_file, 0644);
     return TRUE;
 }
Exemple #5
0
 /**
  * Resizes image
  */
 private function resize($im, $path, $nWidth, $nHeight, $imgInfo)
 {
     $newImg = imagecreateTRUEcolor($nWidth, $nHeight);
     //check if this image is PNG, then set if transparent
     if ($imgInfo[2] === 3) {
         imagealphablending($newImg, FALSE);
         imagesavealpha($newImg, TRUE);
         $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
         imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
     }
     imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
     return $newImg;
 }
Exemple #6
0
 private function imagecreateTRUEcolortransparent($x, $y)
 {
     $i = imagecreateTRUEcolor($x, $y);
     $b = imagecreatefromstring(base64_decode($this->blankpng()));
     imagealphablending($i, FALSE);
     imagesavealpha($i, TRUE);
     imagecopyresized($i, $b, 0, 0, 0, 0, $x, $y, imagesx($b), imagesy($b));
     return $i;
 }
Exemple #7
0
 if (!preg_match_all($pat, $header, $m)) {
     exit;
 }
 // 非图片
 header($m[0][0]);
 $ret = substr($ret, $hsize);
 // 若存在缩略标识且图片宽度超过150则输出缩略图
 if (isset($_GET['t']) && IMGTHUMB && function_exists('gd_info')) {
     $new_w = is_numeric($_GET['t']) ? intval($_GET['t']) : 150;
     // 缩略图宽度
     $photo = imagecreatefromstring($ret);
     $photo_w = ImageSX($photo);
     $photo_h = ImageSY($photo);
     if ($photo_w > $new_w) {
         $new_h = floor($photo_h / ($photo_w / $new_w));
         $thumb = imagecreateTRUEcolor($new_w, $new_h);
         // 创建缩略图的画布
         imagealphablending($thumb, FALSE);
         // 关闭混色模式
         $color = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
         imagefill($thumb, 0, 0, $color);
         // 获取透明色并填充
         imagesavealpha($thumb, TRUE);
         // 保存缩略图时保留完整的 alpha 通道信息
         imagecopyresampled($thumb, $photo, 0, 0, 0, 0, $new_w, $new_h, $photo_w, $photo_h);
         // 进行缩放
         switch ($m[2][0]) {
             // 根据 header 输出特定格式的缩略图
             case 'png':
                 $ret = imagepng($thumb);
                 break;
 function CreateImageHandle($x, $y)
 {
     $this->Debug("Sparkline :: CreateImageHandle({$x}, {$y})", DEBUG_CALLS);
     $handle = @imagecreateTRUEcolor($x, $y);
     if (!is_resource($handle)) {
         $handle = imagecreate($x, $y);
         $this->Debug('imagecreateTRUEcolor unavailable', DEBUG_WARNING);
     }
     if (!is_resource($handle)) {
         $this->Debug('imagecreate unavailable', DEBUG_WARNING);
         $this->Error('could not create image; GD imagecreate functions unavailable');
     }
     return $handle;
 }
 /**
  * Perform resource scaling
  * @param string $file
  * @param string $filename
  * @param string $upload_dir
  * @return bool True is scalling completed without errors
  */
 public function resize($file, $filename, $upload_dir = 'upload')
 {
     // TODO: FIX IT!!!!!!!!
     if (!$this->fs->exists($file)) {
         $file = $upload_dir . '/' . $filename;
     }
     // Check if file exists
     if ($this->fs->exists($file)) {
         // Get file extension
         $fileExtension = $this->fs->extension($file);
         // Read file data and create image handle
         $img = imagecreatefromstring($this->fs->read($file, $filename));
         // Получим текущие размеры картинки
         $sWidth = imagesx($img);
         $sHeight = imagesy($img);
         // Получим соотношение сторон картинки
         $originRatio = $sHeight / $sWidth;
         // Iterate all configured scaling sizes
         foreach ($this->thumnails_sizes as $folder => $size) {
             //trace($folder);
             $folder_path = $upload_dir . '/' . $folder;
             $this->fs->mkDir($folder_path);
             $tHeight = $size['height'];
             $tWidth = $size['width'];
             // Получим соотношение сторон в коробке
             $tRatio = $tHeight / $tWidth;
             if ($tHeight >= $sHeight && $tWidth >= $sWidth) {
                 $width = $sWidth;
                 $height = $sHeight;
             } else {
                 if ($size['fit']) {
                     $correlation = $originRatio < $tRatio;
                 } else {
                     $correlation = $originRatio > $tRatio;
                 }
                 // Сравним соотношение сторон картинки и "целевой" коробки для определения
                 // по какой стороне будем уменьшать картинку
                 if ($correlation) {
                     $width = $tWidth;
                     $height = $width * $originRatio;
                 } else {
                     $height = $tHeight;
                     $width = $height / $originRatio;
                 }
             }
             // Зададим расмер превьюшки
             $new_width = floor($width);
             $new_height = floor($height);
             // Создадим временный файл
             $new_img = imagecreateTRUEcolor($new_width, $new_height);
             if ($fileExtension == "png") {
                 imagealphablending($new_img, false);
                 $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127);
                 imagefill($new_img, 0, 0, $colorTransparent);
                 imagesavealpha($new_img, true);
             } elseif ($fileExtension == "gif") {
                 $trnprt_indx = imagecolortransparent($img);
                 if ($trnprt_indx >= 0) {
                     //its transparent
                     $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
                     $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                     imagefill($new_img, 0, 0, $trnprt_indx);
                     imagecolortransparent($new_img, $trnprt_indx);
                 }
             }
             // Скопируем, изменив размер
             imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $sWidth, $sHeight);
             if (isset($size['crop']) && is_array($size['crop'])) {
                 $crop =& $size['crop'];
                 $cropWidth = $size['width'];
                 $cropHeight = $size['height'];
                 $x = 0;
                 $y = 0;
                 if ($crop['x'] == 'center') {
                     $x = floor(($new_width - $size['width']) / 2);
                 } elseif ($crop['x'] == 'right') {
                     $x = floor($new_width - $size['width']);
                 }
                 if ($crop['y'] == 'middle') {
                     $y = floor(($new_height - $size['height']) / 2);
                 } elseif ($crop['y'] == 'bottom') {
                     $y = floor($new_height - $size['height']);
                 }
                 if ($size['width'] >= $new_width) {
                     $cropWidth = $new_width;
                     $x = 0;
                 }
                 if ($size['height'] >= $new_height) {
                     $cropHeight = $new_height;
                     $y = 0;
                 }
                 $to_crop_array = array('x' => $x, 'y' => $y, 'width' => $cropWidth, 'height' => $cropHeight);
                 $thumb_im = imagecrop($new_img, $to_crop_array);
                 $new_img = $thumb_im;
             }
             // Получим полный путь к превьюхе
             $new_path = $folder_path . '/' . $filename;
             ob_start();
             // Create image handle
             switch (strtolower($fileExtension)) {
                 case 'jpg':
                 case 'jpeg':
                     imagejpeg($new_img, null, isset($size['quality']) ? $size['quality'] : 100);
                     break;
                 case 'png':
                     imagepng($new_img, null);
                     break;
                 case 'gif':
                     imagegif($new_img);
                     break;
                 default:
                     return e('Не поддерживаемый формат изображения[##]!', E_SAMSON_CORE_ERROR, $filename);
             }
             $final_image = ob_get_contents();
             ob_end_clean();
             imagedestroy($new_img);
             // Copy scaled resource
             $this->fs->write($final_image, $filename, $folder_path);
         }
         return true;
     }
     return false;
 }