public static function create($fileName, $module = null, $options = array()) { $uti = new ImageUtility($fileName, $module = null, $options); if ($uti->getError()) { throw new ImageUtilityException($uti->getError()); } return $uti->getObject() ? $uti->getObject() : null; }
public static function make_block9($files, $save, $interlace = null, $jpegQuality = 100) { if (!(count($files) >= 9)) { throw new ImageUtilityException('ImageGdUtility 錯誤!', '參數錯誤,files count:' . count($files), '參數 files 數量一定要大於 9!'); } if (!$save) { throw new ImageUtilityException('ImageGdUtility 錯誤!', '錯誤的儲存路徑,save' . $save, '請再次確認儲存路徑!'); } if (!class_exists('ImageUtility')) { include_once 'ImageUtility.php'; } $positions = array(array('left' => 2, 'top' => 2, 'width' => 130, 'height' => 130), array('left' => 134, 'top' => 2, 'width' => 64, 'height' => 64), array('left' => 200, 'top' => 2, 'width' => 64, 'height' => 64), array('left' => 134, 'top' => 68, 'width' => 64, 'height' => 64), array('left' => 200, 'top' => 68, 'width' => 64, 'height' => 64), array('left' => 2, 'top' => 134, 'width' => 64, 'height' => 64), array('left' => 68, 'top' => 134, 'width' => 64, 'height' => 64), array('left' => 134, 'top' => 134, 'width' => 64, 'height' => 64), array('left' => 200, 'top' => 134, 'width' => 64, 'height' => 64)); $image = imagecreatetruecolor(266, 200); imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255)); for ($i = 0; $i < 9; $i++) { imagecopymerge($image, ImageUtility::create($files[$i])->getImage(), $positions[$i]['left'], $positions[$i]['top'], 0, 0, $positions[$i]['width'], $positions[$i]['height'], 100); } if ($interlace === true) { imageinterlace($image, 1); } else { if ($interlace === false) { imageinterlace($image, 0); } } switch (pathinfo($save, PATHINFO_EXTENSION)) { case 'jpg': return @imagejpeg($image, $save, $jpegQuality); case 'gif': return @imagegif($image, $save); default: case 'png': return @imagepng($image, $save); } }
public function make_block9($files, $save = null, $rawData = true) { if (!(count($files) >= 9)) { throw new ImageUtilityException('ImageImagickUtility 錯誤!', '參數錯誤,files count:' . count($files), '參數 files 數量一定要大於 9!'); } if (!$save) { throw new ImageUtilityException('ImageImagickUtility 錯誤!', '錯誤的儲存路徑,save' . $save, '請再次確認儲存路徑!'); } $newImage = new Imagick(); $newImage->newImage(266, 200, new ImagickPixel('white')); $newImage->setFormat(pathinfo($save, PATHINFO_EXTENSION)); $positions = array(array('left' => 2, 'top' => 2, 'width' => 130, 'height' => 130), array('left' => 134, 'top' => 2, 'width' => 64, 'height' => 64), array('left' => 200, 'top' => 2, 'width' => 64, 'height' => 64), array('left' => 134, 'top' => 68, 'width' => 64, 'height' => 64), array('left' => 200, 'top' => 68, 'width' => 64, 'height' => 64), array('left' => 2, 'top' => 134, 'width' => 64, 'height' => 64), array('left' => 68, 'top' => 134, 'width' => 64, 'height' => 64), array('left' => 134, 'top' => 134, 'width' => 64, 'height' => 64), array('left' => 200, 'top' => 134, 'width' => 64, 'height' => 64)); for ($i = 0; $i < 9; $i++) { $newImage->compositeImage(ImageUtility::create($files[$i])->getImage(), imagick::COMPOSITE_DEFAULT, $positions[$i]['left'], $positions[$i]['top']); } return $newImage->writeImages($save, $rawData); }
protected function calcImageSizeStrict($oldDimension, $newDimension) { $newSize = ImageUtility::createDimension($newDimension['width'], $newDimension['height']); if ($newDimension['width'] >= $newDimension['height']) { if ($oldDimension['width'] > $oldDimension['height']) { $newSize = $this->calcHeight($oldDimension, $newDimension); if ($newSize['width'] < $newDimension['width']) { $newSize = $this->calcWidth($oldDimension, $newDimension); } } else { if ($oldDimension['height'] >= $oldDimension['width']) { $newSize = $this->calcWidth($oldDimension, $newDimension); if ($newSize['height'] < $newDimension['height']) { $newSize = $this->calcHeight($oldDimension, $newDimension); } } } } else { if ($newDimension['height'] > $newDimension['width']) { if ($oldDimension['width'] >= $oldDimension['height']) { $newSize = $this->calcWidth($oldDimension, $newDimension); if ($newSize['height'] < $newDimension['height']) { $newSize = $this->calcHeight($oldDimension, $newDimension); } } else { if ($oldDimension['height'] > $oldDimension['width']) { $newSize = $this->calcHeight($oldDimension, $newDimension); if ($newSize['width'] < $newDimension['width']) { $newSize = $this->calcWidth($oldDimension, $newDimension); } } } } } return $newSize; }
public function save_as($key, $version) { if ($this->error) { return $this->getDebug() ? call_user_func_array('error', $this->error) : array(); } if (!($key && $version)) { return $this->getDebug() ? error('OrmImageUploader 錯誤!', '參數錯誤,請檢查 save_as 函式參數!', '請程式設計者確認狀況!') : array(); } if (!($versions = ($versions = $this->getVersions()) ? $versions : $this->configs['default_version'])) { return $this->getDebug() ? error('OrmImageUploader 錯誤!', 'Versions 格式錯誤,請檢查 getVersions () 或者 default_version!', '預設值 default_version 請檢查 config/system/orm_uploader.php 設定檔!') : array(); } if (in_array($key, $keys = array_keys($versions))) { return $this->getDebug() ? error('OrmImageUploader 錯誤!', '已經有相符合的 key 名稱,key:' . $key, '目前的 key 有:' . implode(', ', $keys)) : array(); } switch ($this->getDriver()) { case 'local': foreach ($versions as $ori_key => $ori_version) { if (is_readable(FCPATH . implode(DIRECTORY_SEPARATOR, $ori_path = array_merge($this->getBaseDirectory(), $this->getSavePath(), array($ori_key . $this->configs['separate_symbol'] . ($name = $this->getValue())))))) { break; } } if (!$ori_path) { return $this->getDebug() ? error('OrmImageUploader 錯誤!', '沒有任何的檔案可以被使用!', '請確認 getVersions () 函式內有存在的檔案可被另存!', '請程式設計者確認狀況!') : array(); } if (!file_exists(FCPATH . implode(DIRECTORY_SEPARATOR, $path = array_merge($this->getBaseDirectory(), $this->getSavePath())))) { $oldmask = umask(0); @mkdir(FCPATH . implode(DIRECTORY_SEPARATOR, $path), 0777, true); umask($oldmask); } if (!is_writable(FCPATH . implode(DIRECTORY_SEPARATOR, $path))) { return $this->getDebug() ? error('OrmImageUploader 錯誤!', '資料夾不能儲存!路徑:' . $path, '請程式設計者確認狀況!') : ''; } try { $image = ImageUtility::create(FCPATH . implode(DIRECTORY_SEPARATOR, $ori_path), null); $path = array_merge($path, array($key . $this->configs['separate_symbol'] . $name)); if ($this->_utility($image, FCPATH . implode(DIRECTORY_SEPARATOR, $path), $key, $version)) { return $path; } else { return array(); } } catch (Exception $e) { return $this->getDebug() ? call_user_func_array('error', $e->getMessages()) : ''; } break; case 's3': if (!@S3::getObject($this->getS3Bucket(), implode(DIRECTORY_SEPARATOR, array_merge($path = array_merge($this->getBaseDirectory(), $this->getSavePath()), array($fileName = array_shift(array_keys($versions)) . $this->configs['separate_symbol'] . ($name = $this->getValue())))), FCPATH . implode(DIRECTORY_SEPARATOR, $fileName = array_merge($this->getTempDirectory(), array($fileName))))) { return $this->getDebug() ? error('OrmImageUploader 錯誤!', '沒有任何的檔案可以被使用!', '請確認 getVersions () 函式內有存在的檔案可被另存!', '請程式設計者確認狀況!') : array(); } try { $image = ImageUtility::create($fileName = FCPATH . implode(DIRECTORY_SEPARATOR, $fileName), null); $newPath = array_merge($path, array($newName = $key . $this->configs['separate_symbol'] . $name)); if ($this->_utility($image, FCPATH . implode(DIRECTORY_SEPARATOR, $newFileName = array_merge($this->getTempDirectory(), array($newName))), $key, $version) && S3::putObjectFile($newFileName = FCPATH . implode(DIRECTORY_SEPARATOR, $newFileName), $this->getS3Bucket(), implode(DIRECTORY_SEPARATOR, $newPath), S3::ACL_PUBLIC_READ, array(), array('Cache-Control' => 'max-age=315360000', 'Expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')))) && @unlink($newFileName) && @unlink($fileName)) { return $newPath; } else { return array(); } } catch (Exception $e) { return $this->getDebug() ? call_user_func_array('error', $e->getMessages()) : ''; } break; } return $this->getDebug() ? error('OrmImageUploader 錯誤!', '未知的 driver,系統尚未支援 ' . $this->getDriver() . ' 的空間!', '請檢查 config/system/orm_uploader.php 設定檔!') : array(); }
/** * Resize an image */ function resize(&$src, &$dest, $dest_width, $dest_height, $src_width, $src_height) { ImageUtility::copyResampled($src, $dest, 0, 0, 0, 0, $dest_width, $dest_width, $src_width, $src_height); }
/** * Return the mime type of the image associated with this ImageInfo object */ function mimeType() { $ret = ImageUtility::imageTypeToMimeType($this->type); return $ret; }
public function save_as($key, $version) { if ($this->error) { return $this->configs['debug'] ? call_user_func_array('error', $this->error) : array(); } if (!($key && $version)) { return $this->configs['debug'] ? error('OrmImageUploader 錯誤!', '參數錯誤,請檢查 save_as 函式參數!', '請程式設計者確認狀況!') : array(); } if (!($versions = ($versions = $this->getVersions()) ? $versions : $this->configs['default_version'])) { return $this->configs['debug'] ? error('OrmImageUploader 錯誤!', 'Versions 格式錯誤,請檢查 getVersions () 或者 default_version!', '預設值 default_version 請檢查 config/system/orm_image_uploader.php 設定檔!') : array(); } switch ($this->configs['bucket']) { case 'local': if (in_array($key, array_keys($versions))) { return is_readable(FCPATH . implode(DIRECTORY_SEPARATOR, $ori_path = array_merge($this->configs['base_directory'][$this->configs['bucket']], $this->getSavePath(), array($key . $this->configs['separate_symbol'] . (string) $this)))) ? $ori_path : ''; } foreach ($versions as $ori_key => $ori_version) { if (is_readable(FCPATH . implode(DIRECTORY_SEPARATOR, $ori_path = array_merge($this->configs['base_directory'][$this->configs['bucket']], $this->getSavePath(), array($ori_key . $this->configs['separate_symbol'] . ($name = (string) $this)))))) { break; } } if (!$ori_path) { return $this->configs['debug'] ? error('OrmImageUploader 錯誤!', '沒有任何的檔案可以被使用!', '請確認 getVersions () 函式內有存在的檔案可被另存!', '請程式設計者確認狀況!') : array(); } if (!file_exists(FCPATH . implode(DIRECTORY_SEPARATOR, $path = array_merge($this->configs['base_directory'][$this->configs['bucket']], $this->getSavePath())))) { $oldmask = umask(0); @mkdir(FCPATH . implode(DIRECTORY_SEPARATOR, $path), 0777, true); umask($oldmask); } if (!is_writable(FCPATH . implode(DIRECTORY_SEPARATOR, $path))) { return $this->configs['debug'] ? error('OrmImageUploader 錯誤!', '資料夾不能儲存!路徑:' . $path, '請程式設計者確認狀況!') : ''; } try { $image = ImageUtility::create(FCPATH . implode(DIRECTORY_SEPARATOR, $ori_path), null); $path = array_merge($path, array($key . $this->configs['separate_symbol'] . $name)); if ($this->_utility($image, FCPATH . implode(DIRECTORY_SEPARATOR, $path), $key, $version)) { return $path; } else { return array(); } } catch (Exception $e) { return $this->configs['debug'] ? call_user_func_array('error', $e->getMessages()) : ''; } break; } return $this->configs['debug'] ? error('OrmImageUploader 錯誤!', '未知的 bucket,系統尚未支援' . $this->configs['bucket'] . ' 的空間!', '請檢查 config/system/orm_image_uploader.php 設定檔!') : ''; }
/** * Write an image to an output path */ function write($image, $output_path, $quality = 100) { switch ($this->info->imageType()) { case Image::GIF: ImageUtility::jpeg($image, $output_path, $quality); break; case Image::JPEG: ImageUtility::jpeg($image, $output_path, $quality); break; case Image::PNG: ImageUtility::png($image, $output_path); break; default: ImageUtility::jpeg($image, $output_path, $quality); break; } }