Exemplo n.º 1
0
function makethumb($srcFile, $dstFile, $dstW, $dstH, $imwf = false)
{
    $im = new imagick($srcFile);
    $source_w = $im->getImageWidth();
    $source_h = $im->getImageHeight();
    if ($dstW === 0 && $dstH === 0) {
        $dstW = $source_w;
        $dstH = $source_h;
    } else {
        // 取得縮在此範圍內的比例
        $percent = getResizePercent($source_w, $source_h, $dstW, $dstH);
        $dstW = $source_w * $percent;
        $dstH = $source_h * $percent;
    }
    $im->thumbnailImage($dstW, $dstH);
    $im->writeImage($dstFile);
    $watermarkFile = './logo.png';
    if ($imwf && is_file($watermarkFile) && is_file($dstFile)) {
        $image = new imagick($dstFile);
        $watermark = new imagick($watermarkFile);
        $iWidth = $image->getImageWidth();
        $iHeight = $image->getImageHeight();
        $wWidth = $watermark->getImageWidth();
        $wHeight = $watermark->getImageHeight();
        if ($iHeight < $wHeight || $iWidth < $wWidth) {
            $watermark->scaleImage($iWidth, $iHeight);
            $wWidth = $watermark->getImageWidth();
            $wHeight = $watermark->getImageHeight();
        }
        $x = ($iWidth - $wWidth) / 2;
        $y = ($iHeight - $wHeight) / 2;
        $image->compositeImage($watermark, imagick::COMPOSITE_OVER, $x, $y);
        $image->writeImage($dstFile);
    }
}
Exemplo n.º 2
0
 private function generate()
 {
     $taken_seats_result = $this->seatbooking->get_taken_seats_ids();
     $taken_seats = array();
     foreach ($taken_seats_result as $seat) {
         $taken_seats[$seat] = 0;
     }
     $coords = $this->load_xmlconfig_to_array();
     $taken_seats_coords = array_intersect_key($coords, $taken_seats);
     $bg = new imagick($this->filename_bg);
     $marker = new imagick($this->filename_marker);
     $marker_width = $marker->getImageWidth();
     $marker_height = $marker->getImageHeight();
     foreach ($taken_seats_coords as $seat) {
         $x = $seat[0] - floor($marker_width / 2);
         $y = $seat[1] - floor($marker_width / 2);
         $bg->compositeImage($marker, imagick::COMPOSITE_OVER, $x, $y);
     }
     return $bg;
 }
Exemplo n.º 3
0
     $image_name .= $letters[mt_rand(0, 61)];
 }
 // не нравится мне расширение jpeg, пусть будет jpg
 if ($p[1] == 'jpeg') {
     $p[1] = 'jpg';
 }
 // полное имя файла изображения
 $image = './images/' . $image_name . '.' . $p[1];
 move_uploaded_file($tmp_image_name, $image);
 // Создаем новый объект imagick
 $im = new imagick($image);
 if ($p[1] != 'gif') {
     // Открываем watermark
     $watermark = new imagick('./img/watermark.png');
     // узнаем размеры оригинального изображения
     $im_width = $im->getImageWidth();
     $im_height = $im->getImageHeight();
     // узнаем размеры водяного знака
     $watermark_width = $watermark->getImageWidth();
     $watermark_height = $watermark->getImageHeight();
     // посчитать x и y
     $left = $im_width - $watermark_width - 10;
     $top = $im_height - $watermark_height - 10;
     // накладываем watermark на оригинальное изображение
     $im->compositeImage($watermark, imagick::COMPOSITE_OVER, $left, $top);
     // сохраняем оригинал
     $im->writeImage('./images/' . $image_name . '.' . $p[1]);
 } else {
     // сохраняем .gif с учетом анимации
     $im->writeImages('./images/' . $image_name . '.' . $p[1], true);
 }
Exemplo n.º 4
0
 function _resizeImagick(&$model, $field, $path, $size, $geometry, $thumbnailPath)
 {
     $srcFile = $path . $model->data[$model->alias][$field];
     $pathInfo = $this->_pathinfo($srcFile);
     $thumbnailType = $this->settings[$model->alias][$field]['thumbnailType'];
     $isMedia = $this->_isMedia($model, $this->runtime[$model->alias][$field]['type']);
     $image = new imagick();
     if ($isMedia) {
         $image->setResolution(300, 300);
         $srcFile = $srcFile . '[0]';
     }
     $image->readImage($srcFile);
     $height = $image->getImageHeight();
     $width = $image->getImageWidth();
     if (preg_match('/^\\[[\\d]+x[\\d]+\\]$/', $geometry)) {
         // resize with banding
         list($destW, $destH) = explode('x', substr($geometry, 1, strlen($geometry) - 2));
         $image->thumbnailImage($destW, $destH);
     } elseif (preg_match('/^[\\d]+x[\\d]+$/', $geometry)) {
         // cropped resize (best fit)
         list($destW, $destH) = explode('x', $geometry);
         $image->cropThumbnailImage($destW, $destH);
     } elseif (preg_match('/^[\\d]+w$/', $geometry)) {
         // calculate heigh according to aspect ratio
         $image->thumbnailImage((int) $geometry - 1, 0);
     } elseif (preg_match('/^[\\d]+h$/', $geometry)) {
         // calculate width according to aspect ratio
         $image->thumbnailImage(0, (int) $geometry - 1);
     } elseif (preg_match('/^[\\d]+l$/', $geometry)) {
         // calculate shortest side according to aspect ratio
         $destW = 0;
         $destH = 0;
         $destW = $width > $height ? (int) $geometry - 1 : 0;
         $destH = $width > $height ? 0 : (int) $geometry - 1;
         $imagickVersion = phpversion('imagick');
         $image->thumbnailImage($destW, $destH, !($imagickVersion[0] == 3));
     }
     if ($isMedia) {
         $thumbnailType = $this->settings[$model->alias][$field]['mediaThumbnailType'];
     }
     if (!$thumbnailType || !is_string($thumbnailType)) {
         try {
             $thumbnailType = $image->getImageFormat();
         } catch (Exception $e) {
             $thumbnailType = 'png';
         }
     }
     $fileName = str_replace(array('{size}', '{filename}'), array($size, $pathInfo['filename']), $this->settings[$model->alias][$field]['thumbnailName']);
     $destFile = "{$thumbnailPath}{$fileName}.{$thumbnailType}";
     $image->setImageCompressionQuality($this->settings[$model->alias][$field]['thumbnailQuality']);
     $image->setImageFormat($thumbnailType);
     if (!$image->writeImage($destFile)) {
         return false;
     }
     $image->clear();
     $image->destroy();
     return true;
 }
Exemplo n.º 5
0
 public function _resizeImagick(Model $model, $field, $path, $size, $geometry, $thumbnailPath)
 {
     $srcFile = $path . $model->data[$model->alias][$field];
     $pathInfo = $this->_pathinfo($srcFile);
     $thumbnailType = $imageFormat = $this->settings[$model->alias][$field]['thumbnailType'];
     $isMedia = $this->_isMedia($model, $this->runtime[$model->alias][$field]['type']);
     $image = new imagick();
     if ($isMedia) {
         $image->setResolution(300, 300);
         $srcFile = $srcFile . '[0]';
     }
     $image->readImage($srcFile);
     $height = $image->getImageHeight();
     $width = $image->getImageWidth();
     if (preg_match('/^\\[[\\d]+x[\\d]+\\]$/', $geometry)) {
         // resize with banding
         list($destW, $destH) = explode('x', substr($geometry, 1, strlen($geometry) - 2));
         $image->thumbnailImage($destW, $destH, true);
         $imageGeometry = $image->getImageGeometry();
         $x = ($imageGeometry['width'] - $destW) / 2;
         $y = ($imageGeometry['height'] - $destH) / 2;
         $image->setGravity(Imagick::GRAVITY_CENTER);
         $image->extentImage($destW, $destH, $x, $y);
     } elseif (preg_match('/^[\\d]+x[\\d]+$/', $geometry)) {
         // cropped resize (best fit)
         list($destW, $destH) = explode('x', $geometry);
         $image->cropThumbnailImage($destW, $destH);
     } elseif (preg_match('/^[\\d]+w$/', $geometry)) {
         // calculate heigh according to aspect ratio
         $image->thumbnailImage((int) $geometry - 1, 0);
     } elseif (preg_match('/^[\\d]+h$/', $geometry)) {
         // calculate width according to aspect ratio
         $image->thumbnailImage(0, (int) $geometry - 1);
     } elseif (preg_match('/^[\\d]+l$/', $geometry)) {
         // calculate shortest side according to aspect ratio
         $destW = 0;
         $destH = 0;
         $destW = $width > $height ? (int) $geometry - 1 : 0;
         $destH = $width > $height ? 0 : (int) $geometry - 1;
         $imagickVersion = phpversion('imagick');
         $image->thumbnailImage($destW, $destH, !($imagickVersion[0] == 3));
     }
     if ($isMedia) {
         $thumbnailType = $imageFormat = $this->settings[$model->alias][$field]['mediaThumbnailType'];
     }
     if (!$thumbnailType || !is_string($thumbnailType)) {
         try {
             $thumbnailType = $imageFormat = $image->getImageFormat();
             // Fix file casing
             while (true) {
                 $ext = false;
                 $pieces = explode('.', $srcFile);
                 if (count($pieces) > 1) {
                     $ext = end($pieces);
                 }
                 if (!$ext || !strlen($ext)) {
                     break;
                 }
                 $low = array('ext' => strtolower($ext), 'thumbnailType' => strtolower($thumbnailType));
                 if ($low['ext'] == 'jpg' && $low['thumbnailType'] == 'jpeg') {
                     $thumbnailType = $ext;
                     break;
                 }
                 if ($low['ext'] == $low['thumbnailType']) {
                     $thumbnailType = $ext;
                 }
                 break;
             }
         } catch (Exception $e) {
             $this->log($e->getMessage(), 'upload');
             $thumbnailType = $imageFormat = 'png';
         }
     }
     $fileName = str_replace(array('{size}', '{filename}', '{primaryKey}'), array($size, $pathInfo['filename'], $model->id), $this->settings[$model->alias][$field]['thumbnailName']);
     $destFile = "{$thumbnailPath}{$fileName}.{$thumbnailType}";
     $image->setImageCompressionQuality($this->settings[$model->alias][$field]['thumbnailQuality']);
     $image->setImageFormat($imageFormat);
     if (!$image->writeImage($destFile)) {
         return false;
     }
     $image->clear();
     $image->destroy();
     return true;
 }