protected function _resize_animated_gif($max_w, $max_h, $crop = false) { $frame_resources = array(); // GD resources of each frame $durations = array(); // ms duration of each frame $gfe = new GifFrameExtractor(); $gfe->extract($this->file); // get frames from gif $dims = image_resize_dimensions($this->size['width'], $this->size['height'], $max_w, $max_h, $crop); if (!$dims) { return new WP_Error('error_getting_dimensions', __('Could not calculate resized image dimensions'), $this->file); } list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; foreach ($gfe->getFrames() as $frame) { $img = $frame["image"]; $duration = $frame["duration"]; $frame_resize_resource = wp_imagecreatetruecolor($dst_w, $dst_h); // resize frame imagecopyresampled($frame_resize_resource, $img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); if (is_resource($frame_resize_resource)) { $frame_resources[] = $frame_resize_resource; $durations[] = $duration; } } if ($frame_resources) { $this->update_size($dst_w, $dst_h); // reconstruct small gif: $gc = new GifCreator(); $gc->create($frame_resources, $durations, 0); return $gc; } else { return new WP_Error('image_resize_error', __('Image resize failed.'), $this->file); } }
public function __construct($filepath) { $this->filepath = $filepath; require_once __DIR__ . '/lib/GifCreator.php'; require_once __DIR__ . '/lib/GifFrameExtractor.php'; if (GifFrameExtractor::isAnimatedGif($this->filepath)) { $gfe = new GifFrameExtractor(); $frames = $gfe->extract(); $newFrames = []; foreach ($frames as $f) { require_once __DIR__ . '/lib/PHPImageWorkshop/ImageWorkshop.php'; $layer = ImageWorkshop::initFromResourceVar($f['image']); //We use resizeScaleWidth in imagestatus //$layer->resizeInPixel($thumbWidth, $thumbHeight, $conserveProportion, $positionX, $positionY, $position); //$layer->resizeByNarrowSideInPixel($newNarrowSideWidth, $conserveProportion); } } }
public function processFolioImage($aFile, $folder, $aName, $imageInfo) { $ext = self::_getImageExt($aFile['type']); if (empty($ext)) { $this->setMessage(iaLanguage::getf('file_type_error', array('extension' => implode(', ', array_unique(self::$_typesMap))))); return false; } $path = IA_UPLOADS . $folder; $image = ImageWorkshop::initFromPath($aFile['tmp_name']); // save source image $image->save($path, self::SOURCE_PREFIX . $aName . $ext); // process thumbnails for files uploaded in CKEditor and other tools if (empty($imageInfo)) { // apply watermark $image = self::_applyWaterMark($image); $image->save($path, self::_createFilename($aName, $ext)); return true; } // check this is an animated GIF if ('image/gif' == $aFile['type'] && $this->iaCore->get('allow_animated_gifs')) { require_once IA_INCLUDES . 'phpimageworkshop' . IA_DS . 'Core' . IA_DS . 'GifFrameExtractor.php'; $gifPath = $aFile['tmp_name']; if (GifFrameExtractor::isAnimatedGif($gifPath)) { // Extractions of the GIF frames and their durations $gfe = new GifFrameExtractor(); $frames = $gfe->extract($gifPath); // For each frame, we add a watermark and we resize it $retouchedFrames = array(); $thumbFrames = array(); foreach ($frames as $frame) { $frameLayer = ImageWorkshop::initFromResourceVar($frame['image']); $thumbLayer = ImageWorkshop::initFromResourceVar($frame['image']); $frameLayer->resizeInPixel($imageInfo['image_width'], $imageInfo['image_height'], true); $frameLayer = self::_applyWaterMark($frameLayer); $retouchedFrames[] = $frameLayer->getResult(); $thumbLayer->resizeInPixel($imageInfo['thumb_width'], $imageInfo['thumb_height'], true); $thumbFrames[] = $thumbLayer->getResult(); } // Then we re-generate the GIF require_once IA_INCLUDES . 'phpimageworkshop' . IA_DS . 'Core' . IA_DS . 'GifCreator.php'; $gc = new GifCreator(); $gc->create($retouchedFrames, $gfe->getFrameDurations(), 0); file_put_contents($path . self::_createFilename($aName, $ext), $gc->getGif()); $thumbCreator = new GifCreator(); $thumbCreator->create($thumbFrames, $gfe->getFrameDurations(), 0); file_put_contents($path . self::_createFilename($aName, $ext, true), $thumbCreator->getGif()); return self::_createFilename($folder . $aName, $ext, true); } } // save full image $largestSide = $imageInfo['image_width'] > $imageInfo['image_height'] ? $imageInfo['image_width'] : $imageInfo['image_height']; if ($largestSide) { $image->resizeByLargestSideInPixel($largestSide, true); } $image = self::_applyWaterMark($image); $image->save($path, self::_createFilename($aName, $ext)); // generate thumbnail $thumbWidth = $imageInfo['thumb_width'] ? $imageInfo['thumb_width'] : $this->iaCore->get('thumb_w'); $thumbHeight = $imageInfo['thumb_height'] ? $imageInfo['thumb_height'] : $this->iaCore->get('thumb_h'); $positionX = $imageInfo['positionX'] ? $imageInfo['positionX'] : 0; $positionY = $imageInfo['positionY'] ? $imageInfo['positionY'] : 0; $position = $imageInfo['position'] ? $imageInfo['position'] : 'MM'; $resize = $imageInfo['resize'] ? $imageInfo['resize'] : 'before_crop'; $cropWidth = $imageInfo['crop_width'] ? $imageInfo['crop_width'] : ''; $cropHeight = $imageInfo['crop_height'] ? $imageInfo['crop_height'] : ''; if ($thumbWidth || $thumbHeight) { $thumb = ImageWorkshop::initFromPath($aFile['tmp_name']); switch ($imageInfo['resize_mode']) { case self::FIT: $thumb->resizeInPixel($thumbWidth, $thumbHeight, true, 0, 0, 'MM'); break; case self::CROP: $largestSide = $thumbWidth > $thumbHeight ? $thumbWidth : $thumbHeight; if ($this->iaCore->get('portfolio_use_crop')) { if ('after_crop' == $resize) { // $thumb->cropMaximumInPixel(0, 0, 'MM'); $thumb->cropInPixel($cropWidth, $cropHeight, $positionX, $positionY, $position); $thumb->resizeInPixel($thumbWidth, $thumbHeight); } else { $thumb->cropMaximumInPixel(0, 0, 'MM'); $thumb->resizeInPixel($largestSide, $largestSide); $thumb->cropInPixel($thumbWidth, $thumbHeight, $positionX, $positionY, $position); } } else { $thumb->cropMaximumInPixel(0, 0, 'MM'); $thumb->resizeInPixel($largestSide, $largestSide); $thumb->cropInPixel($thumbWidth, $thumbHeight, 0, 0, 'MM'); } } $thumb->save($path, self::_createFilename($aName, $ext, true)); } return self::_createFilename($folder . $aName, $ext, true); }