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
-
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 triedconvert /tmp/php2c3dCg -thumbnail 220x220^ -gravity center -extent 220x220 -quality 90 /home/mywebsite/public_html/public/img/posts/thumbnail/1087.jpgit seemed to work. How can I shorten it? -
Is
/tmp/php2c3dCglocated at the root and not inpublic_html? I dont see the folder inpublic_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.