function ConvertImage($Source, $Options = Null) { if (is_string($Options)) { $Options = array('Options' => $Options); } $TargetFolder = GetValue('TargetFolder', $Options, 'uploads/cached', True); $Options = GetValue('Options', $Options); $Filename = CleanupString(pathinfo($Source, 8)) . '-' . Crc32Value($Source, $Options); $Extension = CleanupString(pathinfo($Source, 4)); $ResultImage = $TargetFolder . DS . $Filename . '.' . $Extension; if (!file_exists($ResultImage)) { $Source = GetImageSource($Source); ImageMagick('convert', $Source, $Options, $ResultImage); } return $ResultImage; }
public function save($uri, $quality = 80) { if (empty($this->source)) { return false; } if (!is_dir(dirname($uri))) { mkdir(dirname($uri), 0755, true); } $saved = false; if ($this->magick) { $convert = $this->crop; if ($this->resize) { list($width, $height) = $this->resize; $convert .= " -resize {$width}x{$height}!"; } $image = ImageMagick('convert', "\"{$this->source}\"{$convert} -quality {$quality} -strip -verbose \"{$uri}\""); if ($image['return'] == 0) { $saved = true; } } elseif ($this->type != 'ico') { // in case $this->magick was set to false if (!empty($this->resize)) { list($width, $height) = $this->resize; } elseif (!empty($this->cropped)) { list($width, $height) = array_values($this->cropped); } else { $width = $this->width; $height = $this->height; } $image = imagecreatetruecolor($width, $height); if ($this->type == 'png') { imagealphablending($image, false); imagesavealpha($image, true); $transparent = imagecolorallocatealpha($image, 255, 255, 255, 127); imagefilledrectangle($image, 0, 0, $width, $height, $transparent); } else { $white = imagecolorallocate($image, 255, 255, 255); imagefilledrectangle($image, 0, 0, $width, $height, $white); } switch ($this->type) { case 'gif': $source = imagecreatefromgif($this->source); break; case 'jpg': $source = imagecreatefromjpeg($this->source); break; case 'png': $source = imagecreatefrompng($this->source); break; } $src_x = $this->cropped ? $this->cropped['x1'] : 0; $src_y = $this->cropped ? $this->cropped['y1'] : 0; imagecopyresampled($image, $source, 0, 0, $src_x, $src_y, $width, $height, $this->max('width'), $this->max('height')); switch (substr($uri, strrpos($uri, '.') + 1)) { case 'gif': $saved = imagegif($image, $uri); break; case 'jpeg': case 'jpg': $saved = imagejpeg($image, $uri, $quality); break; case 'png': if ($quality >= 90) { $quality = 0; } else { $quality = abs(round($quality / 10) - 9); } $saved = imagepng($image, $uri, $quality); break; } unset($image, $source); } if ($saved) { $this->saved = $uri; } $this->crop($this->original ? $this->original : array(0, 0, $this->width, $this->height), false); return $saved; }