Пример #1
0
 /**
  * Test initFromString
  */
 public function testInitFromString()
 {
     $layer = ImageWorkshop::initFromString(file_get_contents(__DIR__ . static::IMAGE_SAMPLE_PATH));
     $this->assertTrue(is_object($layer) === true, 'Expect $layer to be an object');
     $this->assertTrue(get_class($layer) === 'PHPImageWorkshop\\Core\\ImageWorkshopLayer', 'Expect $layer to be an ImageWorkshopLayer object');
 }
 /**
  * @param $data
  * @return ImageWorkshopLayer
  *
  * @throws \PHPImageWorkshop\Exception\ImageWorkshopException
  */
 protected function initFromData($data)
 {
     return ImageWorkshop::initFromString($data);
 }
Пример #3
0
 /**
  * Resize a image and returns it's object.
  *
  * @param string  $path
  * @param integer $width
  * @param int     $height
  *
  * @return ImageWorkshopLayer
  */
 public function getResizeMax($path, $width, $height)
 {
     $content = $this->read($path);
     if (!$content) {
         return null;
     }
     $image = \PHPImageWorkshop\ImageWorkshop::initFromString($content);
     $widthOriginal = $image->getWidth();
     $heightOriginal = $image->getHeight();
     if ($width > $widthOriginal && $height > $heightOriginal) {
         return $image;
     }
     $newWidth = null;
     $newHeight = null;
     if ($widthOriginal > $heightOriginal) {
         $newWidth = $width;
     } else {
         $newHeight = $height;
     }
     $image->resizeInPixel($newWidth, $newHeight, true);
     if ($image->getHeight() > $height) {
         $image->resizeInPixel(null, $height, true);
     }
     if ($image->getWidth() > $width) {
         $image->resizeInPixel($width, null, true);
     }
     return $image;
 }
 public function automatic_reduction($file, $image_url)
 {
     $filetype = $this->getFileType($file);
     if ((!empty($this->maximum_picture_size['width']) || !empty($this->maximum_picture_size['height'])) && ($this->width > $this->maximum_picture_size['width'] || $this->height > $this->maximum_picture_size['height'])) {
         if ($this->width > $this->height) {
             $maximum_picture_size_width = empty($this->maximum_picture_size['width']) ? $this->width * $this->maximum_picture_size['height'] / $this->height : $this->maximum_picture_size['width'];
             $new_width = intval($maximum_picture_size_width);
             $new_height = intval($this->height * $maximum_picture_size_width / $this->width);
         } else {
             $maximum_picture_size_height = empty($this->maximum_picture_size['height']) ? $this->height * $this->maximum_picture_size['width'] / $this->width : $this->maximum_picture_size['height'];
             $new_width = intval($this->width * $maximum_picture_size_height / $this->height);
             $new_height = intval($maximum_picture_size_height);
         }
         $layer = ImageWorkshop::initFromString($file);
         if ($filetype == 'gif' && GifFrameExtractor\GifFrameExtractor::isAnimatedGif($this->temp)) {
             $gfe = new GifFrameExtractor\GifFrameExtractor();
             $frames = $gfe->extract($this->temp, true);
             $retouchedFrames = array();
             foreach ($frames as $frame) {
                 $frameLayer = ImageWorkshop::initFromResourceVar($frame['image']);
                 $frameLayer->resizeInPixel($new_width, $new_height);
                 $retouchedFrames[] = $frameLayer->getResult();
             }
             $gc = new GifCreator\GifCreator();
             $gc->create($retouchedFrames, $gfe->getFrameDurations(), 0);
             $file = $gc->getGif();
         } else {
             $layer->resizeInPixel($new_width, $new_height, true);
             ob_start();
             switch ($filetype) {
                 case 'jpg':
                 case 'jpeg':
                     ImageJpeg($layer->getResult(), null, 75);
                     break;
                 case 'png':
                     imagealphablending($layer->getResult(), false);
                     imagesavealpha($layer->getResult(), true);
                     ImagePng($layer->getResult());
                     break;
                 case 'gif':
                     ImageGif($layer->getResult(), null, 75);
                     break;
             }
             $file = ob_get_contents();
             ob_end_clean();
         }
         imagedestroy($layer->getResult());
         $this->width = $new_width;
         $this->height = $new_height;
     }
     return $file;
 }
Пример #5
0
 /**
  * @ApiDoc(
  *  section="File Manager",
  *  description="Displays a (complete) image (with cache-headers)"
  * )
  *
  * @Rest\QueryParam(name="path", requirements=".+", strict=true, description="The file path or its ID")
  *
  * @Rest\Get("/admin/file/image")
  *
  * @param ParamFetcher $paramFetcher
  * @return Response
  */
 public function showImageAction(ParamFetcher $paramFetcher)
 {
     $path = $paramFetcher->get('path');
     if (is_numeric($path)) {
         $path = $this->webFilesystem->getPath($path);
     }
     $this->checkAccess($path, ACL::MODE_VIEW);
     $file = $this->webFilesystem->getFile($path);
     if ($file->isDir()) {
         return;
     }
     $ifModifiedSince = $this->pageStack->getRequest()->headers->get('If-Modified-Since');
     if (isset($ifModifiedSince) && strtotime($ifModifiedSince) == $file->getModifiedTime()) {
         // Client's cache IS current, so we just respond '304 Not Modified'.
         $response = new Response();
         $response->setStatusCode(304);
         $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s', $file->getModifiedTime()) . ' GMT');
         return $response;
     }
     $content = $this->webFilesystem->read($path);
     $image = \PHPImageWorkshop\ImageWorkshop::initFromString($content);
     $result = $image->getResult();
     $size = new FileSize();
     $size->setHandleFromBinary($content);
     $expires = 3600;
     //1 h
     $response = new Response();
     $response->headers->set('Content-Type', 'png' == $size->getType() ? 'image/png' : 'image/jpeg');
     $response->headers->set('Pragma', 'public');
     $response->headers->set('Cache-Control', 'max-age=' . $expires);
     $response->headers->set('Last-Modified', gmdate('D, d M Y H:i:s', $file->getModifiedTime()) . ' GMT');
     $response->headers->set('Expires', gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
     ob_start();
     if ('png' === $size->getType()) {
         imagepng($result, null, 3);
     } else {
         imagejpeg($result, null, 100);
     }
     $response->setContent(ob_get_contents());
     ob_end_clean();
     return $response;
 }