Exemplo n.º 1
0
 public function createVersion($imagePath, $sizeString = false)
 {
     if (strlen($this->urlAlias) < 1) {
         throw new \Exception('Image without urlAlias!');
     }
     $cachePath = $this->getModule()->getCachePath();
     $subDirPath = $this->getSubDur();
     $fileExtension = pathinfo($this->filePath, PATHINFO_EXTENSION);
     if ($sizeString) {
         $sizePart = '_' . $sizeString;
     } else {
         $sizePart = '';
     }
     $pathToSave = $cachePath . '/' . $subDirPath . '/' . $this->urlAlias . $sizePart . '.' . $fileExtension;
     BaseFileHelper::createDirectory(dirname($pathToSave), 0777, true);
     if ($sizeString) {
         $size = $this->getModule()->parseSize($sizeString);
     } else {
         $size = false;
     }
     if ($this->getModule()->graphicsLibrary == 'Imagick') {
         $image = new \Imagick($imagePath);
         $image->setImageCompressionQuality(100);
         if ($size) {
             if ($size['height'] && $size['width']) {
                 $image->cropThumbnailImage($size['width'], $size['height']);
             } elseif ($size['height']) {
                 $image->thumbnailImage(0, $size['height']);
             } elseif ($size['width']) {
                 $image->thumbnailImage($size['width'], 0);
             } else {
                 throw new \Exception('Something wrong with this->module->parseSize($sizeString)');
             }
         }
         $image->writeImage($pathToSave);
     } else {
         $image = new \abeautifulsite\SimpleImage($imagePath);
         if ($size) {
             if ($size['height'] && $size['width']) {
                 //$image->thumbnail($size['width'], $size['height']);
                 $image->best_fit($size['width'], $size['height']);
             } elseif ($size['height']) {
                 $image->fit_to_height($size['height']);
             } elseif ($size['width']) {
                 $image->fit_to_width($size['width']);
             } else {
                 throw new \Exception('Something wrong with this->module->parseSize($sizeString)');
             }
         }
         //WaterMark
         if ($this->getModule()->waterMark) {
             if (!file_exists(Yii::getAlias($this->getModule()->waterMark))) {
                 throw new Exception('WaterMark not detected!');
             }
             $wmMaxWidth = intval($image->get_width() * 0.4);
             $wmMaxHeight = intval($image->get_height() * 0.4);
             $waterMarkPath = Yii::getAlias($this->getModule()->waterMark);
             $waterMark = new \abeautifulsite\SimpleImage($waterMarkPath);
             if ($waterMark->get_height() > $wmMaxHeight or $waterMark->get_width() > $wmMaxWidth) {
                 $waterMarkPath = $this->getModule()->getCachePath() . DIRECTORY_SEPARATOR . pathinfo($this->getModule()->waterMark)['filename'] . $wmMaxWidth . 'x' . $wmMaxHeight . '.' . pathinfo($this->getModule()->waterMark)['extension'];
                 //throw new Exception($waterMarkPath);
                 if (!file_exists($waterMarkPath)) {
                     $waterMark->fit_to_width($wmMaxWidth);
                     $waterMark->save($waterMarkPath, 100);
                     if (!file_exists($waterMarkPath)) {
                         throw new Exception('Cant save watermark to ' . $waterMarkPath . '!!!');
                     }
                 }
             }
             $image->overlay($waterMarkPath, 'bottom right', 0.5, -10, -10);
         }
         $image->save($pathToSave, 100);
     }
     return $image;
 }
Exemplo n.º 2
0
function serve($width, $height, $person)
{
    $app = \Slim\Slim::getInstance();
    $response = $app->response();
    $response['Content-Type'] = 'image/jpeg';
    $img = new abeautifulsite\SimpleImage(getBestImage($width, $height, $person));
    if ($img->get_width() / $img->get_height() >= $width / $height) {
        $img->fit_to_height($height);
        $centre = round($img->get_width() / 2);
        $x1 = $centre - $width / 2;
        $x2 = $centre + $width / 2;
        $img->crop($x1, 0, $x2, $height);
    } else {
        $img->fit_to_width($width);
        $centre = round($img->get_height() / 2);
        $y1 = $centre - $height / 2;
        $y2 = $centre + $height / 2;
        $img->crop(0, $y1, $width, $y2);
    }
    $img = applyFilters($img);
    $img->output();
}
Exemplo n.º 3
0
 public function createVersion($imagePath, $sizeString = false, $crop = true, $fitImageInCanvas = false)
 {
     if (strlen($this->urlAlias) < 1) {
         throw new \Exception('Image without urlAlias!');
     }
     $cachePath = $this->getModule()->getCachePath();
     $subDirPath = $this->getSubDur();
     $fileExtension = pathinfo($this->filePath, PATHINFO_EXTENSION);
     if ($sizeString) {
         $sizePart = '_' . $sizeString;
     } else {
         $sizePart = '';
     }
     $pathToSave = $cachePath . '/' . $subDirPath . '/' . $this->urlAlias . $sizePart . '.' . $fileExtension;
     BaseFileHelper::createDirectory(dirname($pathToSave), 0777, true);
     if ($sizeString) {
         $size = $this->getModule()->parseSize($sizeString);
     } else {
         $size = false;
     }
     if ($this->getModule()->graphicsLibrary == 'Imagick') {
         // Fixes interlaced images
         $interlaceFix = $this->interlaceFix($imagePath);
         if ($interlaceFix) {
             $image = new \Imagick();
             $image->readImageBlob($interlaceFix);
         } else {
             $image = new \Imagick($imagePath);
         }
         $image->setImageCompressionQuality(100);
         // Fixes image rotations
         $this->ImagickAutoRotateImage($image);
         // If the dimensions of the original image match the requested
         // dimensions the original image is just copied to the new path
         if ($image->getImageWidth() == $size['width'] && $image->getImageHeight() == $size['height']) {
             copy($imagePath, $pathToSave);
             return $image;
         }
         if ($size) {
             if ($size['height'] && $size['width']) {
                 if (!$crop && $fitImageInCanvas) {
                     $image = $this->fitImageInCanvas($image, $size['width'], $size['height']);
                 } elseif ($crop) {
                     $image->cropThumbnailImage($size['width'], $size['height']);
                 } else {
                     $image->resizeImage($size['width'], $size['height'], \Imagick::FILTER_HAMMING, 1, false);
                 }
             } elseif ($size['height']) {
                 $image->thumbnailImage(0, $size['height']);
             } elseif ($size['width']) {
                 $image->thumbnailImage($size['width'], 0);
             } else {
                 throw new \Exception('Something wrong with this->module->parseSize($sizeString)');
             }
         }
         $image->writeImage($pathToSave);
     } else {
         $image = new \abeautifulsite\SimpleImage($imagePath);
         // If the dimensions of the original image match the requested
         // dimensions the original image is just copied to the new path
         if ($image->get_width() == $size['width'] && $image->get_height() == $size['height']) {
             copy($imagePath, $pathToSave);
             return $image;
         }
         if ($size) {
             if ($size['height'] && $size['width']) {
                 if ($crop) {
                     $image->thumbnail($size['width'], $size['height']);
                 } else {
                     $image->resize($size['width'], $size['height']);
                 }
             } elseif ($size['height']) {
                 $image->fit_to_height($size['height']);
             } elseif ($size['width']) {
                 $image->fit_to_width($size['width']);
             } else {
                 throw new \Exception('Something wrong with this->module->parseSize($sizeString)');
             }
         }
         //WaterMark
         if ($this->getModule()->waterMark) {
             if (!file_exists(Yii::getAlias($this->getModule()->waterMark))) {
                 throw new Exception('WaterMark not detected!');
             }
             $wmMaxWidth = intval($image->get_width() * 0.4);
             $wmMaxHeight = intval($image->get_height() * 0.4);
             $waterMarkPath = Yii::getAlias($this->getModule()->waterMark);
             $waterMark = new \abeautifulsite\SimpleImage($waterMarkPath);
             if ($waterMark->get_height() > $wmMaxHeight or $waterMark->get_width() > $wmMaxWidth) {
                 $waterMarkPath = $this->getModule()->getCachePath() . DIRECTORY_SEPARATOR . pathinfo($this->getModule()->waterMark)['filename'] . $wmMaxWidth . 'x' . $wmMaxHeight . '.' . pathinfo($this->getModule()->waterMark)['extension'];
                 //throw new Exception($waterMarkPath);
                 if (!file_exists($waterMarkPath)) {
                     $waterMark->fit_to_width($wmMaxWidth);
                     $waterMark->save($waterMarkPath, 100);
                     if (!file_exists($waterMarkPath)) {
                         throw new Exception('Cant save watermark to ' . $waterMarkPath . '!!!');
                     }
                 }
             }
             $image->overlay($waterMarkPath, 'bottom right', 0.5, -10, -10);
         }
         $image->save($pathToSave, 100);
     }
     return $image;
 }
Exemplo n.º 4
0
 private function save_action_image($new_file_name)
 {
     ini_set('memory_limit', '256M');
     include './third_part/SimpleImage.php';
     try {
         $img = new abeautifulsite\SimpleImage($this->user_action_uploads_dir . $new_file_name);
         $wim = $img->get_width();
         $him = $img->get_height();
         if ($wim > 1000 && $him > 250) {
             $img->fit_to_width(740)->save($this->user_action_save_dir . '740_' . $new_file_name);
             $img->fit_to_width(200)->save($this->user_action_save_dir . '200_' . $new_file_name);
             if (file_exists($this->user_action_save_dir . '740_' . $new_file_name) && file_exists($this->user_action_save_dir . '200_' . $new_file_name)) {
                 unlink($this->user_action_uploads_dir . $new_file_name);
                 echo json_encode(array('file_name' => $new_file_name, "success" => 1));
             } else {
                 echo json_encode(array("error" => -1, 'error_message' => 'unknown_file'));
             }
         } else {
             unlink($this->user_action_uploads_dir . $new_file_name);
             echo json_encode(array("error" => -1, 'error_message' => 'size_error'));
         }
     } catch (Exception $e) {
         echo json_encode(array("error" => -1, 'error_message' => $e->getMessage()));
     }
 }