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()); }
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); }
/** * @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; }
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); }
/** * @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; }
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); } } } } }