public function run($class_name) { $path = realpath(Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . $class_name); $class_name = ucfirst($class_name); if ($path && is_dir($path) && is_writable($path)) { $dir = key($_GET); $filename = $_GET[$dir]; $pk = pathinfo($filename, PATHINFO_FILENAME); $image = Images::model()->findByPk($pk); if ($image != null) { $image->resize($dir); } } elseif (class_exists($class_name)) { $dir = key($_GET); $filename = $_GET[$dir]; $size = explode('x', $dir); $path = realpath(Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . $class_name); if (YII_DEBUG && !file_exists($path . DIRECTORY_SEPARATOR . $dir)) { mkdir($path . DIRECTORY_SEPARATOR . $dir, 0777); } if ($path !== FALSE && file_exists($path . DIRECTORY_SEPARATOR . $dir) && is_file($path . DIRECTORY_SEPARATOR . $filename) && $size[0] > 0 && $size[1] > 0) { Yii::import('ext.iwi.Iwi'); $image = new Iwi($path . DIRECTORY_SEPARATOR . $filename); $image->adaptive($size[0], $size[1]); $image->save($path . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . $filename, 0644, TRUE); $mime = CFileHelper::getMimeType($path . DIRECTORY_SEPARATOR . $filename); header('Content-Type: ' . $mime); $image->render(); exit; } } return parent::run($class_name); }
/** * Resize image to this size * * @param string $thumb @see $this->previews */ public function resize($thumb) { Yii::import('ext.iwi.Iwi'); $folder = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . 'photos'; $file = $this->id . '.' . $this->extension; //$folder = DIRECTORY_SEPARATOR . trim($folder, DIRECTORY_SEPARATOR . ' '); if (!isset($this->previews[$thumb])) { return 0; } $size = $this->previews[$thumb]; if (!is_file($folder . DIRECTORY_SEPARATOR . $file)) { return 0; } $out_file = $folder . DIRECTORY_SEPARATOR . $thumb . DIRECTORY_SEPARATOR . $file; //output file if (is_file($out_file)) { return $out_file; } $image = new Iwi($folder . DIRECTORY_SEPARATOR . $file); if ($size[$this->WIDTH] == 0) { $image->resize(0, $size[$this->HEIGHT], Image::HEIGHT); } elseif ($size[$this->HEIGHT] == 0) { $image->resize($size[$this->WIDTH], 0, Image::WIDTH); } else { $image->adaptive($size[$this->WIDTH], $size[$this->HEIGHT])->crop($size[$this->WIDTH], $size[$this->HEIGHT], 'top', 'center')->sharpen(10); } if (!file_exists($folder . DIRECTORY_SEPARATOR . $thumb)) { mkdir($folder . DIRECTORY_SEPARATOR . $thumb, 777); } $image->save($out_file); return $out_file; }
public static function AddContent($imageFile, $name, $type, $targetRatio = null) { if ($targetRatio == null) { $targetRatio = 4 / 3; } if (is_string($imageFile)) { $pathParts = pathinfo($imageFile); $originalExtension = strtolower($pathParts['extension']); $imageFileName = $imageFile; } else { $pathParts = pathinfo($imageFile->getName()); $originalExtension = strtolower($pathParts['extension']); $imageFileName = 'temp' . uniqid() . '.' . $originalExtension; $imageFile->saveAs(Yii::app()->params['temp'] . '/' . $imageFileName); } $content = new Content(); $content->Content_name = $name; $content->Content_type = $type; $content->save(); $content->refresh(); $path = Yii::app()->params['uploads'] . '/' . $content->Content_ID; $link = Yii::app()->params['siteBase'] . '/uploads/' . $content->Content_ID; $path .= '.' . $originalExtension; $link .= '.' . $originalExtension; rename(Yii::app()->params['temp'] . '/' . $imageFileName, $path); $image = new Iwi($path); $sourceRatio = $image->width / $image->height; if ($sourceRatio > $targetRatio) { $height = $image->height; if ($height > Content::MAX_HEIGHT) { $height = Content::MAX_HEIGHT; } $width = (int) $height * $targetRatio; } elseif ($sourceRatio < $targetRatio) { $width = $image->width; if ($width > Content::MAX_WIDTH) { $width = Content::MAX_WIDTH; } $height = (int) $width / $targetRatio; } else { $height = $image->height; $width = $image->width; if ($height > Content::MAX_HEIGHT) { $height = Content::MAX_HEIGHT; $width = (int) $height * $targetRatio; } } $image->adaptive($width, $height); $image->save(); $content->Link = $link; $content->save(); return $content; }
public function resize($folder, $file, $sizes) { $previews = $this->previews; if (is_string($sizes)) { $previews = isset($this->previews[$sizes]) ? $this->previews[$sizes] : $this->previews; } else { if (is_array($sizes)) { $previews = array_intersect_key($this->previews, array_fill_keys($sizes, '1')); } } if ($folder === null) { $folder = Yii::getPathOfAlias('webroot') . '/upload/photos'; } if ($file === null) { $file = $this->id . '.' . $this->extension; } $folder = '/' . trim($folder, '/ '); var_dump($previews); exit; foreach ($this->previews as $sub => $size) { if (is_file($folder . '/' . $sub . '/' . $file)) { continue; } $image = new Iwi($folder . '/' . $file); if ($size[$this->WIDTH] == 0) { $image->resize(0, $size[$this->HEIGHT], Image::HEIGHT); } elseif ($size[$this->HEIGHT] == 0) { $image->resize($size[$this->WIDTH], 0, Image::WIDTH); } else { $image->adaptive($size[$this->WIDTH], $size[$this->HEIGHT])->crop($size[$this->WIDTH], $size[$this->HEIGHT], 'top', 'center')->sharpen(10); } if (!file_exists($folder . '/' . $sub)) { mkdir($folder . '/' . $sub, 777); } $image->save($folder . '/' . $sub . '/' . $file); } }