Пример #1
0
 /**
  * This method calculates the image and delivers it to the client.
  *
  * @param $folder
  * @param $file
  * @param $width
  * @param $height
  * @param $mode
  */
 public function resize($folder, $file, $width = -1, $height = -1, $mode = 'nocrop')
 {
     $jpeg_orientation_translation = array(1 => 0, 2 => 0, 3 => 180, 4 => 0, 5 => 0, 6 => -90, 7 => 0, 8 => 90);
     /**
      * @var JApplicationSite $app
      */
     $app = JFactory::getApplication();
     $params = $app->getParams();
     if (strcmp($mode, 'full') == 0) {
         $mode = 'nocrop';
         $width = COM_EVENTGALLERY_IMAGE_ORIGINAL_MAX_WIDTH;
         $height = COM_EVENTGALLERY_IMAGE_ORIGINAL_MAX_WIDTH;
     }
     if ($height > $width) {
         $width = $height;
     }
     $sizeSet = new EventgalleryHelpersSizeset();
     $saveAsSize = $sizeSet->getMatchingSize($width);
     $file = STR_REPLACE("\\.\\.", "", $file);
     $folder = STR_REPLACE("\\.\\.", "", $folder);
     $width = STR_REPLACE("\\.\\.", "", $width);
     $mode = STR_REPLACE("\\.\\.", "", $mode);
     $file = STR_REPLACE("/", "", $file);
     $folder = STR_REPLACE("/", "", $folder);
     $width = STR_REPLACE("/", "", $width);
     $mode = STR_REPLACE("/", "", $mode);
     $file = STR_REPLACE("\\", "", $file);
     $folder = STR_REPLACE("\\", "", $folder);
     $width = STR_REPLACE("\\", "", $width);
     $mode = STR_REPLACE("\\", "", $mode);
     $basedir = COM_EVENTGALLERY_IMAGE_FOLDER_PATH;
     $sourcedir = $basedir . $folder;
     $cachebasedir = COM_EVENTGALLERY_IMAGE_CACHE_PATH;
     $cachedir = $cachebasedir . $folder;
     $cachedir_thumbs = $cachebasedir . $folder;
     if (!is_dir(JPATH_CACHE)) {
         //mkdir($cachebasedir, 0777);
         mkdir(JPATH_CACHE);
     }
     if (!is_dir($cachebasedir)) {
         //mkdir($cachebasedir, 0777);
         mkdir($cachebasedir);
     }
     if (!is_dir($cachedir)) {
         //mkdir($cachedir, 0777);
         mkdir($cachedir);
     }
     if (!is_dir($cachedir_thumbs)) {
         //mkdir($cachedir_thumbs, 0777);
         mkdir($cachedir_thumbs);
     }
     $image_file = $sourcedir . DIRECTORY_SEPARATOR . $file;
     $image_thumb_file = $cachedir_thumbs . DIRECTORY_SEPARATOR . $mode . $saveAsSize . $file;
     //$last_modified = gmdate('D, d M Y H:i:s T', filemtime ($image_file));
     $last_modified = gmdate('D, d M Y H:i:s T', mktime(0, 0, 0, 1, 1, 2100));
     #echo "<br>".$image_thumb_file."<br>";
     $debug = false;
     if ($debug || !file_exists($image_thumb_file)) {
         $ext = pathinfo($image_file, PATHINFO_EXTENSION);
         $input_jpeg = null;
         $exif = null;
         if (strtolower($ext) == "gif") {
             if (!($im_original = imagecreatefromgif($image_file))) {
                 echo "Error opening {$image_file}!";
                 exit;
             }
         } else {
             if (strtolower($ext) == "jpg" || strtolower($ext) == "jpeg") {
                 // try to use PEL first. If things fail, use the php internal method to get the JPEG
                 try {
                     $input_jpeg = new PelJpeg($image_file);
                     /* Retrieve the original Exif data in $jpeg (if any). */
                     $exif = $input_jpeg->getExif();
                     /* The input image is already loaded, so we can reuse the bytes stored
                      * in $input_jpeg when creating the Image resource. */
                     if (!($im_original = ImageCreateFromString($input_jpeg->getBytes()))) {
                         echo "Error opening {$image_file}!";
                         exit;
                     }
                 } catch (Exception $e) {
                     if (!($im_original = imagecreatefromjpeg($image_file))) {
                         echo "Error opening {$image_file}!";
                         exit;
                     }
                 }
             } else {
                 if (strtolower($ext) == "png") {
                     if (!($im_original = imagecreatefrompng($image_file))) {
                         echo "Error opening {$image_file}!";
                         exit;
                     }
                 } else {
                     die("{$ext} not supported");
                 }
             }
         }
         if ($params->get('use_autorotate', 1) == 1 && $exif != NULL) {
             $tiff = $exif->getTiff();
             $ifd0 = $tiff->getIfd();
             $orientation = $ifd0->getEntry(PelTag::ORIENTATION);
             if ($orientation != null) {
                 $im_original = imagerotate($im_original, $jpeg_orientation_translation[$orientation->getValue()], 0);
                 $orientation->setValue(1);
             }
         }
         $orig_width = imagesx($im_original);
         $orig_height = imagesy($im_original);
         $orig_ratio = imagesx($im_original) / imagesy($im_original);
         $sizeCalc = new EventgalleryHelpersSizecalculator($orig_width, $orig_height, $width, strcmp('crop', $mode) == 0);
         $height = $sizeCalc->getHeight();
         $width = $sizeCalc->getWidth();
         //print_r($sizeCalc);
         // create canvas/border image
         //adjust height to not enlarge images
         if ($width > $orig_width) {
             $width = $orig_width;
         }
         if ($height > $orig_height) {
             $height = $orig_height;
         }
         if (strcmp('crop', $mode) != 0) {
             $canvasWidth = $width;
             $canvasHeight = ceil($width / $orig_ratio);
             if ($canvasHeight > $height) {
                 $canvasHeight = $height;
                 $canvasWidth = ceil($height * $orig_ratio);
             }
             $width = $canvasWidth;
             $height = $canvasHeight;
         } else {
             $height = $width;
         }
         $isOriginalSize = false;
         if ($height == $orig_height && $width == $orig_width) {
             $isOriginalSize = true;
         }
         /**
          * Do not recalculate the image if we don't need to resize it.
          */
         if ($isOriginalSize && $params->get('use_sharpening_for_originalsize', 1) == 0) {
             $im_output = $im_original;
         } else {
             $im_output = imagecreatetruecolor($width, $height);
             $resize_faktor = $orig_height / $height;
             $new_height = $height;
             $new_width = $orig_width / $resize_faktor;
             if ($new_width < $width) {
                 $resize_faktor = $orig_width / $width;
                 $new_width = $width;
                 $new_height = $orig_height / $resize_faktor;
             }
             imagecopyresampled($im_output, $im_original, $width / 2 - $new_width / 2, $height / 2 - $new_height / 2, 0, 0, $new_width, $new_height, $orig_width, $orig_height);
             $use_sharpening = $params->get('use_sharpening', 1);
             if ($use_sharpening == 1) {
                 // configure the sharpening
                 $stringSharpenMatrix = $params->get('image_sharpenMatrix', '[[-1,-1,-1],[-1,16,-1],[-1,-1,-1]]');
                 $sharpenMatrix = json_decode($stringSharpenMatrix);
                 if (null == $sharpenMatrix || count($sharpenMatrix) != 3) {
                     $sharpenMatrix = array(array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1));
                 }
                 $divisor = array_sum(array_map('array_sum', $sharpenMatrix));
                 $offset = 0;
                 if (function_exists('imageconvolution')) {
                     if (version_compare(phpversion(), '5.5.9', '=')) {
                         $this->imageconvolution($im_output, $sharpenMatrix, $divisor, $offset);
                     } else {
                         imageconvolution($im_output, $sharpenMatrix, $divisor, $offset);
                     }
                 }
             }
         }
         /**
          * @var EventgalleryLibraryManagerFolder $folderMgr
          * @var EventgalleryLibraryFolder $folder
          */
         $folderMgr = EventgalleryLibraryManagerFolder::getInstance();
         $folder = $folderMgr->getFolder($folder);
         $watermark = $folder->getWatermark();
         // load default watermark
         if (null == $watermark || !$watermark->isPublished()) {
             /**
              * @var EventgalleryLibraryManagerWatermark $watermarkMgr
              * @var EventgalleryLibraryWatermark $watermark
              */
             $watermarkMgr = EventgalleryLibraryManagerWatermark::getInstance();
             $watermark = $watermarkMgr->getDefaultWatermark();
         }
         if (null != $watermark && $watermark->isPublished()) {
             $watermark->addWatermark($im_output);
         }
         $image_quality = $params->get('image_quality', 85);
         if ($input_jpeg != null) {
             Pel::setJPEGQuality($image_quality);
             /* We want the raw JPEG data from $scaled. Luckily, one can create a
              * PelJpeg object from an image resource directly: */
             $output_jpeg = new PelJpeg($im_output);
             /* If no Exif data was present, then $exif is null. */
             if ($exif != null) {
                 $output_jpeg->setExif($exif);
             }
             /* We can now save the scaled image. */
             $writeSuccess = true;
             $output_jpeg->saveFile($image_thumb_file);
         } else {
             $writeSuccess = imagejpeg($im_output, $image_thumb_file, $image_quality);
             if (!$writeSuccess) {
                 die("Unable to write to file {$image_thumb_file}");
             }
         }
         if (!$writeSuccess) {
             die("Unable to write to file {$image_thumb_file}");
         }
         $time = time() + 315360000;
         touch($image_thumb_file, $time);
         // add the ICC profile
         try {
             $o = new JPEG_ICC();
             $o->LoadFromJPEG($image_file);
             $o->SaveToJPEG($image_thumb_file);
         } catch (Exception $e) {
         }
     }
     $mime = ($mime = getimagesize($image_thumb_file)) ? $mime['mime'] : $mime;
     $size = filesize($image_thumb_file);
     $fp = fopen($image_thumb_file, "rb");
     if (!($mime && $size && $fp)) {
         // Error.
         return;
     }
     if (!$debug) {
         header("Content-Type: " . $mime);
         header("Content-Length: " . $size);
         header("Last-Modified: {$last_modified}");
     }
     fpassthru($fp);
     die;
     //$app->close();
 }
Пример #2
0
 /**
  * Copy the source images' ICC Profile (color profile) to the new file in the cache
  *
  * @since 2.0
  * @uses PHP JPEG ICC profile manipulator
  * @param string $cacheFilePath
  * @return string contents of image
  *
  * @link http://jpeg-icc.sourceforge.net/
  * @link http://sourceforge.net/projects/jpeg-icc/
  */
 private function copyICCProfile($cacheFilePath)
 {
     require_once dirname(__FILE__) . '/../icc/class.jpeg_icc.php';
     try {
         $o = new JPEG_ICC();
         $o->LoadFromJPEG($this->getSource()->getFullPath());
         $o->SaveToJPEG($cacheFilePath);
     } catch (Exception $e) {
     }
     return file_get_contents($cacheFilePath);
 }
Пример #3
0
 } else {
     if (file_exists($midsize)) {
         $d->alternate($midsize);
     }
 }
 if ($hires) {
     $d->retina();
 }
 if (!$settings['retain_image_metadata'] || max($w, $h) < 480 || $settings['image_processing_library'] === 'gd') {
     // Work around issue with mbstring.func_overload = 2
     if (ini_get('mbstring.func_overload') & 2 && function_exists('mb_internal_encoding')) {
         $previous_encoding = mb_internal_encoding();
         mb_internal_encoding('ISO-8859-1');
     }
     require $root . $ds . 'app' . $ds . 'koken' . $ds . 'icc.php';
     $icc = new JPEG_ICC();
     preg_match('~^(/\\d{3}/\\d{3}/)~', $path, $match);
     $icc_cache_key = 'icc' . $match[1] . 'profile.icc';
     $icc_cache = Shutter::get_cache($icc_cache_key);
     if ($icc_cache) {
         $icc->SetProfile($icc_cache['data']);
     } else {
         $icc->LoadFromJpeg($original);
         Shutter::write_cache($icc_cache_key, $icc->GetProfile());
     }
     $d->strip();
 }
 $blob = $d->render();
 if (isset($icc)) {
     $blob = $icc->SaveToBlob($blob);
 }