/** * {@inheritDoc} */ public function save($item, $key = '') { $id = $item['id']; $outputType = strtolower($this->getParm('type')); $path = ($this->publicPath ? $this->publicPath . DIRECTORY_SEPARATOR : '') . $this->getParm('path'); $compression = $this->getParm('compression'); $key = ($key ? $key . '.' : '') . $this->name; $file = $this->request->file($key); if ($file) { if ($file->isValid()) { $tmpFile = $path . DIRECTORY_SEPARATOR . $id; $outputFile = $tmpFile . '.' . $outputType; $file->move($path, $id); list($width, $height, $inputType) = getimagesize($tmpFile); if ($inputType === imageConvertType($outputType)) { rename($tmpFile, $outputFile); } else { imageSave($outputType, $outputFile, imageOpen($inputType, $tmpFile), $compression); unlink($tmpFile); } } } elseif (!$this->request->input($key)) { $this->delete($item); } }
/** * Saves image with given resource. * * @param int $type * @param string $file * @param resource $image * @param int $compression */ function imageSave($type, $file, $image, $compression = 80) { $type = imageConvertType($type); if ($type === IMAGETYPE_JPEG) { imagejpeg($image, $file, $compression); } elseif ($type === IMAGETYPE_GIF) { imagegif($image, $file); } elseif ($type === IMAGETYPE_PNG) { imagepng($image, $file); } }