Example #1
0
 public function testInitializeWithBinaryData()
 {
     $imagedata = file_get_contents(__DIR__ . '/../resources/test.jpg');
     self::assertNotNull($imagedata);
     $thumb = new GD($imagedata);
     self::assertSame(array('width' => 500, 'height' => 375), $thumb->getCurrentDimensions());
 }
Example #2
0
 public function getThumb($path, $variant = 'default')
 {
     $var = $this->variants[$variant];
     if (!$var) {
         throw new \Exception('Incorrect thumb type: ' . $variant);
     }
     $filePath = $this->getThumbPath($path, $variant);
     if (file_exists($filePath) && is_file($filePath)) {
         return realpath($filePath);
     }
     try {
         $thumb = new GD($path);
     } catch (\Exception $e) {
         return false;
     }
     $operation = $var['operation'] ?: 'resize';
     $thumb->{$operation}($var['maxWidth'], $var['maxHeight']);
     $thumb->setOptions(['jpegQuality' => $var['quality']]);
     $dir = dirname($filePath);
     if (!file_exists($dir) || !is_dir($dir)) {
         mkdir($dir, 0755, true);
     }
     $thumb->save($filePath, $var['format']);
     return realpath($filePath);
 }
Example #3
0
 public function testIfWillResizeSavedImage()
 {
     // Temp file location
     $fileName = __DIR__ . '/logo-1024.png';
     $md5 = md5_file($fileName);
     $model = new ModelWithEmbeddedFile();
     $model->file = new Image();
     $model->file->set($fileName);
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     /* @var $found ModelWithEmbeddedFile */
     $file = $found->file->get()->getBytes();
     $this->assertSame($fileName, $found->file->filename);
     $this->assertSame($md5, md5($file));
     $image = $found->file;
     $params = new ImageParams();
     $params->width = 100;
     $params->height = 100;
     /* @var $image Image */
     $scaledName = tempnam('/tmp/', 'image-test') . '.png';
     $image->get($params)->write($scaledName);
     $this->assertTrue(file_exists($scaledName));
     $gd = new GD($scaledName);
     $dimensions = (object) $gd->getCurrentDimensions();
     codecept_debug($dimensions);
     $this->assertSame($params->width, $dimensions->width);
     $this->assertSame($params->height, $dimensions->height);
 }
Example #4
0
 /**
  * @param \PHPThumb\GD $phpthumb
  * @return \PHPThumb\GD
  */
 public function execute($phpthumb)
 {
     $currentDimensions = $phpthumb->getCurrentDimensions();
     $watermarkDimensions = $this->wm->getCurrentDimensions();
     $watermarkPositionX = $this->offsetX;
     $watermarkPositionY = $this->offsetY;
     if (preg_match('/right|east/i', $this->position)) {
         $watermarkPositionX += $currentDimensions['width'] - $watermarkDimensions['width'];
     } else {
         if (!preg_match('/left|west/i', $this->position)) {
             $watermarkPositionX += intval($currentDimensions['width'] / 2 - $watermarkDimensions['width'] / 2);
         }
     }
     if (preg_match('/bottom|lower|south/i', $this->position)) {
         $watermarkPositionY += $currentDimensions['height'] - $watermarkDimensions['height'];
     } else {
         if (!preg_match('/upper|top|north/i', $this->position)) {
             $watermarkPositionY += intval($currentDimensions['height'] / 2 - $watermarkDimensions['height'] / 2);
         }
     }
     $workingImage = $phpthumb->getWorkingImage();
     $watermarkImage = $this->wm->getWorkingImage() ? $this->wm->getWorkingImage() : $this->wm->getOldImage();
     $this->imageCopyMergeAlpha($workingImage, $watermarkImage, $watermarkPositionX, $watermarkPositionY, 0, 0, $watermarkDimensions['width'], $watermarkDimensions['height'], $this->opacity);
     $phpthumb->setWorkingImage($workingImage);
     return $phpthumb;
 }
Example #5
0
 public function __toString()
 {
     if ($this->adaptive) {
         $this->gd->adaptiveResize($this->width, $this->height);
     } else {
         $this->gd->resize($this->width, $this->height);
     }
     return sprintf('data:%s;base64,%s', $this->gd->getFormat(), base64_encode($this->gd->getImageAsString()));
 }
Example #6
0
 protected function _set($tempName, $fileName, $params = [])
 {
     if ($this->isImage($fileName)) {
         $thumb = new GD($tempName);
         $dimensions = (object) $thumb->getCurrentDimensions();
         $this->width = $dimensions->width;
         $this->height = $dimensions->height;
     }
     parent::_set($tempName, $fileName, $params);
 }
Example #7
0
 /**
  * @param \PHPThumb\GD $phpthumb
  * @return \PHPThumb\GD
  */
 public function execute($phpthumb)
 {
     $currentImage = $phpthumb->getOldImage();
     $currentDimensions = $phpthumb->getCurrentDimensions();
     $borderTop = 0;
     $borderBottom = 0;
     $borderLeft = 0;
     $borderRight = 0;
     if (in_array('T', $this->sides)) {
         for (; $borderTop < $currentDimensions['height']; ++$borderTop) {
             for ($x = 0; $x < $currentDimensions['width']; ++$x) {
                 if (imagecolorat($currentImage, $x, $borderTop) != $this->rgb2int($this->color)) {
                     break 2;
                 }
             }
         }
     }
     if (in_array('B', $this->sides)) {
         for (; $borderBottom < $currentDimensions['height']; ++$borderBottom) {
             for ($x = 0; $x < $currentDimensions['width']; ++$x) {
                 if (imagecolorat($currentImage, $x, $currentDimensions['height'] - $borderBottom - 1) != $this->rgb2int($this->color)) {
                     break 2;
                 }
             }
         }
     }
     if (in_array('L', $this->sides)) {
         for (; $borderLeft < $currentDimensions['width']; ++$borderLeft) {
             for ($y = 0; $y < $currentDimensions['height']; ++$y) {
                 if (imagecolorat($currentImage, $borderLeft, $y) != $this->rgb2int($this->color)) {
                     break 2;
                 }
             }
         }
     }
     if (in_array('R', $this->sides)) {
         for (; $borderRight < $currentDimensions['width']; ++$borderRight) {
             for ($y = 0; $y < $currentDimensions['height']; ++$y) {
                 if (imagecolorat($currentImage, $currentDimensions['width'] - $borderRight - 1, $y) != $this->rgb2int($this->color)) {
                     break 2;
                 }
             }
         }
     }
     $newWidth = $currentDimensions['width'] - ($borderLeft + $borderRight);
     $newHeight = $currentDimensions['height'] - ($borderTop + $borderBottom);
     $newImage = imagecreatetruecolor($newWidth, $newHeight);
     imagecopy($newImage, $currentImage, 0, 0, $borderLeft, $borderTop, $currentDimensions['width'], $currentDimensions['height']);
     $phpthumb->setOldImage($newImage);
     $phpthumb->setCurrentDimensions(array('width' => $newWidth, 'height' => $newHeight));
     return $phpthumb;
 }
Example #8
0
 /**
  * Creates image thumbnails
  */
 public function createThumbs()
 {
     $path = $this->getUploadedFilePath($this->attribute);
     foreach ($this->thumbs as $profile => $config) {
         $thumbPath = static::getThumbFilePath($this->attribute, $profile);
         if (!is_file($thumbPath)) {
             /** @var GD $thumb */
             $thumb = new GD($path);
             $thumb->adaptiveResize($config['width'], $config['height']);
             FileHelper::createDirectory(pathinfo($thumbPath, PATHINFO_DIRNAME), 0775, true);
             $thumb->save($thumbPath);
         }
     }
 }
 public function createThumbs()
 {
     $path = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . $this->resolvePath();
     foreach ($this->thumbs as $profile => $config) {
         $thumbPath = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . $this->resolveThumbPath($profile);
         if (!is_file($thumbPath)) {
             /** @var GD $thumb */
             $thumb = new GD($path);
             if (isset($config['resizeUp'])) {
                 $thumb->setOptions(array('resizeUp' => $config['resizeUp']));
             }
             if (isset($config['adaptive']) && $config['adaptive'] === false) {
                 $thumb->resize($config['width'], $config['height']);
             } else {
                 //By default we do adaptiveResize
                 $thumb->adaptiveResize($config['width'], $config['height']);
             }
             @mkdir(pathinfo($thumbPath, PATHINFO_DIRNAME), 777, true);
             $thumb->save($thumbPath);
         }
     }
 }
 public function createThumbs()
 {
     $path = $this->getUploadedFilePath($this->attribute);
     if (file_exists($path)) {
         foreach ($this->thumbs as $profile => $config) {
             $thumbPath = static::getThumbFilePath($this->attribute, $profile);
             if (!is_file($thumbPath)) {
                 /** @var GD $thumb */
                 $thumb = new GD($path);
                 $dimensions = $thumb->getCurrentDimensions();
                 if ($dimensions['width'] > $dimensions['height']) {
                     $thumb->resize($config['width'], $config['height']);
                 } else {
                     $thumb->resize($config['height'], $config['width']);
                 }
                 FileHelper::createDirectory(pathinfo($thumbPath, PATHINFO_DIRNAME), 0775, true);
                 $thumb->save($thumbPath);
                 if (isset($config['watermark'])) {
                     $watermarkedImage = Image::watermark($thumbPath, $config['watermark']);
                     $watermarkedImage->save($thumbPath);
                 }
             }
         }
     }
 }
Example #11
0
 public function render($view = 'index', $data = array())
 {
     $view = urldecode($view);
     $contentPath = $this->getOwner()->getContentPath();
     $rootPath = $this->getOwner()->getRootPath();
     $matches = [];
     if (preg_match('~@\\((\\d+)x(\\d+)\\)([a-z]{1})?$~', $view, $matches)) {
         $this->width = $matches[1];
         $this->height = $matches[2];
         if (isset($matches[3])) {
             $this->options = preg_split('~~', $matches[3], PREG_SPLIT_NO_EMPTY);
         }
         $pattern = preg_quote($matches[0]);
         $baseView = preg_replace("~{$pattern}\$~", '', $view);
     }
     $thumbName = sprintf('%s/%s.%s', $rootPath, $view, $this->extension);
     // Get thumbnail dir for later use
     $thumbDir = dirname($thumbName);
     // Try to make a thumbnail dir
     if (!file_exists($thumbDir)) {
         @mkdir($thumbDir, 0777, true);
     }
     $baseExt = str_replace('thumb.', '', $this->extension);
     $fileName = sprintf('%s/%s.%s', $contentPath, $baseView, $baseExt);
     if (!file_exists($fileName)) {
         throw new NotFoundException(sprintf('File not found: `%s`', $fileName));
     }
     if (!file_exists($thumbName)) {
         $image = new GD($fileName);
         if (in_array(self::OptionCrop, $this->options)) {
             $image->adaptiveResize($this->width, $this->height);
         } else {
             $image->resize($this->width, $this->height);
         }
         // ensure we can write into dir or overwrite a file
         if (is_writeable($thumbDir) || is_writeable($thumbName)) {
             $image->save($thumbName);
         }
     }
     $info = new SplFileInfo($thumbName);
     $size = $info->getSize();
     if ($size > 0) {
         header(sprintf('Content-Length: %d', $size));
     }
     switch (strtolower($info->getExtension())) {
         case 'gif':
             header('Content-type: image/gif');
             break;
         case 'jpg':
             header('Content-type: image/jpeg');
             break;
         case 'png':
         case 'string':
             header('Content-type: image/png');
             break;
     }
     header(sprintf('ETag: %s', md5($thumbName)));
     header(sprintf('Last-Modified: %s', gmdate('D, d M Y H:i:s \\G\\M\\T', $info->getMTime())));
     header(sprintf('Content-Disposition: filename="%s"', basename($fileName)));
     // Cache it
     header('Pragma: public');
     header('Cache-Control: max-age=86400');
     header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 86400));
     echo file_get_contents($thumbName);
     exit;
 }
Example #12
0
 /**
  * @param Idea $idea
  * @param      $fileData
  * @param null $alternativeName
  */
 public function addFileToIdea(Idea $idea, $fileData, $alternativeName = null)
 {
     //Parse first the type of file to see in which table it should be stored
     $mimeType = new MimeType();
     $mimeType->isValid($fileData);
     /*
      * Use the fileSize validator to validate the size
      */
     $fileSize = new FilesSize(PHP_INT_MAX);
     $fileSize->isValid($fileData);
     $contentType = $this->getGeneralService()->findContentTypeByContentTypeName($fileData['type']);
     switch ($mimeType->type) {
         case 'application/pdf':
             //All treated as document
         //All treated as document
         case 'application/msword':
             //All treated as document
         //All treated as document
         case 'application/vnd.ms-excel':
             //All treated as document
         //All treated as document
         case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
             //All treated as document
         //All treated as document
         case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
             //All treated as document
         //All treated as document
         case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
             $ideaDocument = new Document();
             $ideaDocument->setIdea($idea);
             $ideaDocument->setFilename($fileData['name']);
             $ideaDocument->setSize($fileSize->size);
             if (is_null($alternativeName)) {
                 $ideaDocument->setDocument($fileData['name']);
             } else {
                 $ideaDocument->setDocument($alternativeName);
             }
             $ideaDocumentObject = new DocumentObject();
             $ideaDocumentObject->setObject(file_get_contents($fileData['tmp_name']));
             $ideaDocument->setContentType($contentType);
             $ideaDocumentObject->setDocument($ideaDocument);
             $this->newEntity($ideaDocumentObject);
             break;
         case 'image/jpeg':
         case 'image/png':
             $ideaImage = new Image();
             $ideaImage->setIdea($idea);
             if (is_null($alternativeName)) {
                 $ideaImage->setImage($fileData['name']);
             } else {
                 $ideaImage->setImage($alternativeName);
             }
             $ideaImage->setSize($fileSize->size);
             $ideaImage->setContentType($contentType);
             $ideaImageObject = new ImageObject();
             $ideaImageObject->setObject(file_get_contents($fileData['tmp_name']));
             $thumb = new GD($fileData['tmp_name']);
             $thumb->resize(200);
             $ideaImageObject->setThumb($thumb->getImageAsString());
             $ideaImageObject->setImage($ideaImage);
             $this->newEntity($ideaImageObject);
             break;
     }
 }