简体   繁体   English

使用PHP的ImageMagick和上传的文件

[英]ImageMagick and Uploaded File using PHP

I am handling a uploading image file using PHP, then using ImageMagick to create a thumbnail of the image. 我正在使用PHP处理上传的图像文件,然后使用ImageMagick创建图像的缩略图。

Problem: How should I provide Imagemagick with the path of the source and destination file? 问题:如何为Imagemagick提供源文件和目标文件的路径?

I gave the location of the source file as $_FILES['photo']['tmp_name']; 我将源文件的位置指定为$_FILES['photo']['tmp_name']; which turns out to be something like 原来是这样的

/tmp/php2c3dCg

For the destination file, I had to provide the full path like 对于目标文件,我必须提供完整路径,例如

/home/mywebsite/public_html/public/img/posts/thumbnail/1087_1174cc2995db2a19472da1bbf1665e94b5121246.jpg

Question

  1. Do I have to give the full path to the destination image? 我是否必须提供目标图像的完整路径? In the examples on Imagemagick website, it used convert dragon.gif -resize 64x64 resize_dragon.gif . 在Imagemagick网站上的示例中,它使用了convert dragon.gif -resize 64x64 resize_dragon.gif When I tried convert /tmp/php2c3dCg -thumbnail 220x220^ -gravity center -extent 220x220 -quality 90 /home/mywebsite/public_html/public/img/posts/thumbnail/1087.jpg it seemed to work. 当我尝试convert /tmp/php2c3dCg -thumbnail 220x220^ -gravity center -extent 220x220 -quality 90 /home/mywebsite/public_html/public/img/posts/thumbnail/1087.jpg它似乎可以工作。 How can I shorten it? 如何缩短呢?

  2. Is /tmp/php2c3dCg located at the root and not in public_html ? /tmp/php2c3dCg位于根目录而不位于public_html I dont see the folder in public_html . 我在public_html中看不到该文件夹​​。

1. 1。

You can use paths relative to the working directory. 您可以使用相对于工作目录的路径。 If you are using PHP's exec() , the working directory is the directory containing the PHP script. 如果您使用的是PHP的exec() ,则工作目录是包含PHP脚本的目录。 But what's wrong with absolute paths, your script doesn't care if they are long and it's one less source of problems. 但是绝对路径有什么问题,您的脚本并不关心它们是否长,并且它是问题的根源。

2. 2。

Yes, it is in the system's temp directory. 是的,它在系统的临时目录中。 This is where PHP puts user uploaded files. 这是PHP放置用户上传文件的地方。 They are deleted after the request is processed, so you need to copy/process it and save it somewhere else, if you want to keep it. 处理完请求后,它们将被删除,因此,如果要保留请求,则需要复制/处理并将其保存在其他位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM