开发者

PHP :to display image with passthru is it better then create image in disk

i need display modyfied image , i have 2 option as i see it to create temp image that is modified and then delete it or to create the image on the fly and display it with passthru for example :

$photo="foo.jpg";
$THUMB_SZ  = 125;
$THUMB_PRESZ  = $THUMB_SZ * 2;
$QUALITY = 87;
$convert = "/usr/bin/convert";
$command = "$convert -size $THUMB_PRESZ".'x'."$THUMB_PRESZ \"$photo\"" .
        " -thumbnail $THUMB_SZ".'x'."$THUMB_SZ" .
        " -unsharp 0.2x0.6+1.0" .
        " -quality $QUAL开发者_开发技巧ITY JPG:-";

header("Content-type: image/jpeg");
passthru($command, $retval);

and then in the html part <img src="foo.php">


If you need to create the image more than once, then I would suggest you create the file on disk, for two reasons

  • It saves creating it more than once.
  • With the correct caching headers, you can save transferring the data to the same client more than once as well.

If you really only need to show it once, ever, then you can do it using passthrough (or, if you're interested in performance, use the PHP Imagick bindings, it's faster, cleaner and safer than using imagick via the command line).


If you don't need to keep the modified image then using passthru saves a disk write and a delete. Your app is probably not that speed sensitive though.


If I really concern for performance, I would avoid the passthru choice. It would needlessly execute external command each time a user requested the image. If you use temp image, and changed the img src reference to the temp image, only the web server will retrieve the image and send it to the user, no PHP nor convert will be involved. Of course, I was assuming the image is changed not very often.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜