简体   繁体   中英

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.

Problem: How should I provide Imagemagick with the path of the source and destination file?

I gave the location of the source file as $_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 . 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. How can I shorten it?

  2. Is /tmp/php2c3dCg located at the root and not in public_html ? I dont see the folder in public_html .

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. 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.

Yes, it is in the system's temp directory. This is where PHP puts user uploaded files. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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