I try to to save an image which was created with imagemagick in php. This is my code:
$source = str_replace(' ','%20',$_GET['src']);
$uri_components = parse_url($_GET['src']);
$width = isset($_GET['w']) ? abs(intval($_GET['w'])) : 0;
$height = isset($_GET['h']) ? abs(intval($_GET['h'])) : 0;
$zc = isset($_GET['zc']) ? intval($_GET['zc']) : 0;
$q = isset($_GET['q']) ? intval($_GET['q']) : 0;
if(isset($uri_components['host']) && !in_array($uri_components['host'], $allowed_domains)) {
$bad_domain = true;
if(ALLOW_SUBDOMAINS === true)
foreach($allowed_domains as $domain)
if(preg_match('/\.' . str_replace('.', '\.', $domain) . '$/i', $uri_components['host'])) {
$bad_domain = false;
break;
}
if($bad_domain)
trigger_error('Host not allowed: ' . $uri_components['host'], E_USER_ERROR);
}
if(isset($uri_components['host']) || file_exists($source))
$image_handle = fopen($source, 'rb');
else if(file_exists($_SERVER['DOCUMENT_ROOT'] . $source))
$image_handle = fopen($_SERVER['DOCUMENT_ROOT'] .$source, 'rb');
else
trigger_error('Image file (src) not found', E_USER_ERROR);
$image = new Imagick();
$image->readimagefile($image_handle);
if($zc)
try {
$image->cropthumbnailimage($width, $height);
} catch (ImagickException $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
}
else
try {
$image->thumbnailimage($width, $height, true);
开发者_运维知识库 } catch(ImagickException $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
}
//$q = 100;
$image->setImageCompression($compression_type);
$image->setImageCompressionQuality($q);
$image->setResolution(300, 300);
$cache_time = 157680000;
header('Cache-Control: max-age=' . $cache_time . ',must-revalidate');
header('Expires: ' . gmdate("D, d M Y H:i:s", time() + $cache_time));
header('Content-Type: image/' . $image->getImageFormat());
echo $image;
$name = 'cache/'.$filename.'.'.$image->getImageFormat();
file_put_contents($name, file_get_contents($image));
The Image is requested in that way:
/script.php?src=pic.jpg&w=100&h=100&zc=1&q=90
There seems to be a problem with the file header. if I open an Image locally, I get an error, that the header is incorrect. Where is the Mistake in my function?
Thanks, Lars
Actually in that code, you don't need to do file_get_contents($image)
, because $image
ALREADY is the content that you need.
I think that the last line should be:
file_put_contents($name, $image);
精彩评论