©
This document uses PHP Chinese website manual Release
(PECL imagick 0.9.0-0.9.9)
Imagick::writeImage — 把图片写入指定的文件
$filename
= NULL
] )把图片写入指定的文件。如果指定文件参数的值是 NULL ,图片将会写入通过 Imagick::readImage() 或者 Imagick::setImageFilename() 设定的文件。
filename
图片将被写入的文件名。文件的后缀指定了文件的类型。 可以强制指定图片的格式而不管文件的后缀名。在文件名前面加上一个前缀,例如 “jpg:test.png”。
成功时返回 TRUE
。
[#1] Anonymous [2014-11-13 17:16:44]
What happens if the file already exists??? Does it get overwritten?
[#2] Anonymous [2014-07-11 10:25:16]
I opened image with Imagick::readImageBlob. When I wanted to save resized image Imagick::writeImage did not work, but Imagick::writeImageFile did.
$image = new Imagick();
$image->readImageBlob($image_data);
// $image->writeImage($fileDst) did not work
if($f=fopen($fileDst, "w")){
$image->writeImageFile($f);
}
[#3] SkepticaLee [2014-03-27 14:30:09]
When running Imagick as packaged with the Abyss Web Server, neither this method nor writeImages () works. Instead the format has to be declared and the file saved by using another method or procedure, e.g.:
<?php
$im = new Imagick ();
$im->newImage (300, 225, "blue");
$im->writeImage ("test_0.jpg"); // fails with no error message
//instead
$im->setImageFormat ("jpeg");
file_put_contents ("test_1.jpg", $im); // works, or:
$im->imageWriteFile (fopen ("test_2.jpg", "wb")); //also works
?>
[#4] pfz at pfzone dot org [2012-11-24 17:47:48]
With Imagick 3.1.0RC2, PHP4.8
If you plan to overwrite the file you're working on, before doing writeImage, consider clearing the file buffer before the write statement :
<?php
$image = new Imagick($your_file);
clearstatcache(dirname($your_file));
// or
unlink($your_file);
$image->writeImage($your_file);
?>
It happened to me that the resulting file size was wrong. This could lead to truncation, as the file is not expanded.
This happened while working on JPEG, and PNG.
this line worked for me without this hack.
<?php
file_put_contents($your_file, $image);
?>
Do not rely on getImageLength() for sending your image, especially when keepalive is ON. Content length is then relevant, and must be set. If the wrong size is given, your image will be truncated.
Use filesize($your_file_) once written or strlen($image) instead (which renders your image and updates getImageLength() result).
[#5] icinagle at gmail dot com [2011-05-23 18:47:01]
If you are trying to manipulate a uploaded file and then save the file all in the same request with Apache + mod_dav this will fail.
mod_dav puts a lock on the file during the request where the file is uploaded so trying to save the smallest file, e.g. 1kb will fail with a "Failed to allocate memory" error.