/** * Perfoms manipulation * * @param string $from * @param string $to * @param string $options * @return void */ public function manipulate($from, $to, $options) { if (false === class_exists('Thumbnail')) { throw new Yag_Manipulator_Adapter_Exception('Class Thumbnail could not be loaded'); } if (!isset($options['geometry'])) { throw new Yag_Manipulator_Adapter_Exception('Thumbnail requires the \'geometry\' option to be set'); } $matches = array(); preg_match('/(c)?([0-9]+)x([0-9]+)/', $options['geometry'], $matches); $crop = empty($matches[1]) ? false : true; $width = $matches[2]; $height = $matches[3]; if (empty($matches[2])) { throw new Yag_Manipulator_Adapter_Exception('Invalid geometry pattern \'' . $options['geometry'] . '\''); } if (empty($matches[3])) { throw new Yag_Manipulator_Adapter_Exception('Invalid geometry pattern \'' . $options['geometry'] . '\''); } $thumbnail = new Thumbnail($from); // TODO: Fix error handling around this... $quality = 80; if (false == $crop) { $thumbnail->resize($width, $height); $quality = 100; } else { if ($width == $height) { // Well works for now... the crop for ImageTransform is a bit better // but who cares? $thumbnail->cropFromCenter($width); } else { $thumbnail->crop(0, 0, $width, $height); } } $thumbnail->save($to, $quality); }
function crop($imagePath, $thumbnailPath, $dimensions) { $crop = false; $newSize = trim(intval($dimensions[0])) > 0 ? trim(intval($dimensions[0])) : 100; $thumb = new Thumbnail($imagePath); if ($thumb->error) { echo $imagePath . ":" . $thumb->errmsg . "<br />"; return false; } $minLength = min($thumb->getCurrentWidth(), $thumb->getCurrentHeight()); $maxLength = max($thumb->getCurrentWidth(), $thumb->getCurrentHeight()); // Image is smaller than the specified size so we just rename it and save if ($maxLength <= $newSize) { $thumb->save($thumbnailPath, $this->quality); //Just rename and save without processing } else { // At least one side is larger than specified thumbnail size // Both sides are larger than resize length so first we scale and if image is not square we crop if ($minLength > $newSize) { // Scale smaller size to desired new size if ($thumb->getCurrentWidth() < $thumb->getCurrentHeight()) { $thumb->resize($newSize, 0); $crop = true; } elseif ($thumb->getCurrentWidth() > $thumb->getCurrentHeight()) { $thumb->resize(0, $newSize); $crop = true; } else { $thumb->resize($newSize, $newSize); } if ($crop) { $thumb->cropFromCenter($newSize); } // One size is smaller than the new size, so we only crop the larger size to the new size } else { $cropX = intval(($thumb->getCurrentWidth() - $newSize) / 2); $cropY = intval(($thumb->getCurrentHeight() - $newSize) / 2); $thumb->crop($cropX, $cropY, $newSize, $newSize); } $thumb->save($thumbnailPath, $this->quality); } $thumb->destruct(); if (file_exists($thumbnailPath)) { return true; } return false; }