Fishcakes Posted March 25, 2021 Share Posted March 25, 2021 Hi I'm trying to resize pictures for thumbnails I can do this correctly at linux command line with the following command convert ideologies.jpeg -resize 300x200 ./Thumbnails/testphp.jpg However trying to wrap this in PHP doesn't seem to work // Upload file to server if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ // Insert image file name into database $insert = $conn->query("INSERT into Threads (Title, ThreadBody, filename) VALUES ('$Title', '$BodyText', '$fileName')"); if($insert){ $statusMsg = "The file ".$fileName. " has been uploaded successfully."; exec("convert {['fileName']} -resize 300x200 wankertestphp.jpg"); } If you look above I do successfully hit the "The File has been uploaded successfully" line so it is entering that If statement. I've also checked the Threads table with a Select * from Threads and I can see the $fileName variable is entering there. I've tried numerous different ways around the $fileName variable (removing the brackets...removing the brackets and the apostrophes") Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/312382-using-the-exec-function-to-contain-an-imagemagick-command/ Share on other sites More sharing options...
gw1500se Posted March 25, 2021 Share Posted March 25, 2021 Have you tried this? exec("convert -resize 300x200",$targetFilePath,wankertestphp.jpg); Quote Link to comment https://forums.phpfreaks.com/topic/312382-using-the-exec-function-to-contain-an-imagemagick-command/#findComment-1585386 Share on other sites More sharing options...
Fishcakes Posted March 25, 2021 Author Share Posted March 25, 2021 I tried this but it didn't work If you enter that command at command line terminal like convert -resize 300x200",$targetFilePath,wankertestphp.jpg It doesn't do anything Quote Link to comment https://forums.phpfreaks.com/topic/312382-using-the-exec-function-to-contain-an-imagemagick-command/#findComment-1585392 Share on other sites More sharing options...
requinix Posted March 25, 2021 Share Posted March 25, 2021 Couple issues here. First is the matter of putting variables into strings. "{['filename']}" is just not going to work at all. I mean, there's gotta be a $ somewhere. Second is that you have to use the right values for convert. Because of the move_uploaded_file, you need to convert $targetFilePath. Not whatever "filename" was supposed to be. Then, just like with variables in SQL queries, you need to make sure that the $targetFilePath doesn't cause problems with the convert command. There is no "prepared commands" concept like SQL has so you have to escape it yourself - fortunately there's a built-in function to do that. $targetFilePathArg = escapeshellarg($targetFilePath); exec("convert $targetFilePathArg -resize 300x200 ./Thumbnails/testphp.jpg"); Quote Link to comment https://forums.phpfreaks.com/topic/312382-using-the-exec-function-to-contain-an-imagemagick-command/#findComment-1585393 Share on other sites More sharing options...
Fishcakes Posted March 25, 2021 Author Share Posted March 25, 2021 (edited) Thanks I did try that and it reached the correct IF statement to produce "The file has been uploaded successfully" However it did not create the file. I even amended your statement to $targetFilePathArg = escapeshellarg($targetFilePath); exec("convert $targetFilePathArg -resize 300x200 .jpg"); Just to check it was not failing at finding the folder for some reason. Bigger block of code below // File upload path $targetDir = "upload/"; $fileName = basename($_FILES["file"]["name"]); $targetFilePath = $targetDir . $fileName; $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION); if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){ // Allow certain file formats $allowTypes = array('jpg','png','jpeg','gif','pdf'); if(in_array($fileType, $allowTypes)){ // Upload file to server if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){ // Insert image file name into database $insert = $conn->query("INSERT into Threads (Title, ThreadBody, filename) VALUES ('$Title', '$BodyText', '$fileName')"); if($insert){ $statusMsg = "The file ".$fileName. " has been uploaded successfully."; $targetFilePathArg = escapeshellarg($targetFilePath); exec("convert $targetFilePathArg -resize 300x200 .jpg"); }else{ $statusMsg = "File upload failed, please try again."; } Edited March 25, 2021 by Fishcakes Quote Link to comment https://forums.phpfreaks.com/topic/312382-using-the-exec-function-to-contain-an-imagemagick-command/#findComment-1585394 Share on other sites More sharing options...
requinix Posted March 25, 2021 Share Posted March 25, 2021 5 minutes ago, Fishcakes said: I even amended your statement to $targetFilePathArg = pathinfo($targetFilePath,PATHINFO_EXTENSION); exec("convert $targetFilePathArg -resize 300x200 testingresize.jpg"); That's very much not correct. Go back to what I posted. You might be encountering file permission problems. 1. Decide what directory you want to put the thumbnails in. If you're using upload/ then I suggest either (a) upload/thumbnails or (b) changing the filenames to be like example.jpg for the original and example.thumb.jpg for the resized version, and they both go in upload/. 2. If you want a subdirectory, create it and then chmod 0777 it - or do whatever it is you did with upload/ to allow copying the file there. Because I'm 80% certain you did have to do something. Also, checking if the $_FILES "name" isn't empty is not enough. Look at the "error" and make sure it has the value UPLOAD_ERR_OK. If not then maybe there was no file (UPLOAD_ERR_NO_FILE) or there was some other problem during the upload. Quote Link to comment https://forums.phpfreaks.com/topic/312382-using-the-exec-function-to-contain-an-imagemagick-command/#findComment-1585395 Share on other sites More sharing options...
Fishcakes Posted March 25, 2021 Author Share Posted March 25, 2021 Yes I did chmod the folder. After uploading my internet page displays the latest upload so I'm seeing it retrieved by PHP onto my site as well as seeing it in the folder. What is the best way to error check in php? At the moment I'm just doing php upload.php to check it in linux terminal For exec to work do I did a debian package installed? Quote Link to comment https://forums.phpfreaks.com/topic/312382-using-the-exec-function-to-contain-an-imagemagick-command/#findComment-1585396 Share on other sites More sharing options...
requinix Posted March 25, 2021 Share Posted March 25, 2021 8 minutes ago, Fishcakes said: What is the best way to error check in php? At the moment I'm just doing php upload.php to check it in linux terminal Best way to do what? 8 minutes ago, Fishcakes said: For exec to work do I did a debian package installed? Is that a question? Because no, I don't believe you have to install anything to use exec(): it's part of the core PHP and it's not something Linux distros separate out into installable packages (as opposed to stuff like JSON, which is "core" but often separated). Quote Link to comment https://forums.phpfreaks.com/topic/312382-using-the-exec-function-to-contain-an-imagemagick-command/#findComment-1585397 Share on other sites More sharing options...
Fishcakes Posted March 26, 2021 Author Share Posted March 26, 2021 (edited) OK I added some error checking $output=null; $retval=null; exec("convert $targetFilePathArg -resize 300x200 testresize.jpg",$output, $retval); echo "REturned with status $retval and output:\n" ; if ($retval == null) { echo "Retval is null\n" ; which returns REturned with status 1 and output: The file test.jpg has been uploaded successfully. status 1 would be an error no? I also then went to create a php file with just this in <?php exec("convert arrow.png -resize 300x200 ./testresize.jpg",$output, $retval); ?> Then run this at terminal and I get the following. convert-im6.q16: unable to open image `testresize.jpg': Permission denied @ error/blob.c/OpenBlob/2874. But I've already provided permissions to this folder via chmod -R 707 upload Edited March 26, 2021 by Fishcakes Quote Link to comment https://forums.phpfreaks.com/topic/312382-using-the-exec-function-to-contain-an-imagemagick-command/#findComment-1585401 Share on other sites More sharing options...
Fishcakes Posted March 26, 2021 Author Share Posted March 26, 2021 Not to worry I managed to figure it out. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/312382-using-the-exec-function-to-contain-an-imagemagick-command/#findComment-1585402 Share on other sites More sharing options...
PrincePHP Posted September 30, 2022 Share Posted September 30, 2022 On 3/26/2021 at 5:06 PM, Fishcakes said: Not to worry I managed to figure it out. Cheers Please share with us, even i am looking for this answer. Quote Link to comment https://forums.phpfreaks.com/topic/312382-using-the-exec-function-to-contain-an-imagemagick-command/#findComment-1601216 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.