djpic Posted February 19, 2008 Share Posted February 19, 2008 I am trying take a photo that is from a user (uploaded via a form) compress is (resize it) and then save it to a directory. I have the part in which I can copy the file to a directory on the server, I just need a simple code to resize the image to a size I specify. For example: $image = $_FILE['userfile']['tmp_name']; $image = (function it resize the image) I have done a search on code to do this, but all the ones I find is just too complicated for what I am trying to do. I know I need to use GD to do it. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/91986-solved-gd-help/ Share on other sites More sharing options...
Barand Posted February 20, 2008 Share Posted February 20, 2008 Sorry, but as I have no idea of what "too complicated" is for you, I don't know what to suggest. Any resize is going to involve some sort of proportional scaling algorithm. Link to comment https://forums.phpfreaks.com/topic/91986-solved-gd-help/#findComment-471229 Share on other sites More sharing options...
djpic Posted February 20, 2008 Author Share Posted February 20, 2008 I know how to do the scaling algorithm. That is not a big deal. That is the problem, all the samples of code I have seen have this crazy scaling algorithm. I all I want it just the basic lines of code or commands that allow me to pull the file from the user, scale it (don't worry about the algorithm), and then save the file to a folder on the server. Link to comment https://forums.phpfreaks.com/topic/91986-solved-gd-help/#findComment-471255 Share on other sites More sharing options...
djpic Posted February 20, 2008 Author Share Posted February 20, 2008 Ok here is what I got. Now I just need to know a few things. First: it saved the file in the test directory but does not save the compressed file in the directory, just copies the original one. How can I get it to save the compressed or resized image in the test directory? I tried just changing the: move_uploaded_file($_FILES['userfile']['tmp_name'], $Destination) To: move_uploaded_file($image_resized, $Destination) But didn't work. Second: When it does save the file, the permissions are set to 600 instead of 777. How can I fix this? Here is the code: <?php // Load image $UserImage = $_FILES['userfile']['tmp_name']; $image = @imagecreatefromjpeg($UserImage); // Get original width and height $width = imagesx($image); $height = imagesy($image); // New width and height $new_width = 150; $new_height = 100; // Resample $image_resized = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); $Directory = 'test/'; $Destination = $Directory . basename($_FILES['userfile']['name']); // Upload Image echo '<pre>'; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $Destination)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible file upload attack!\n"; } echo 'Here is some more debugging info:'; print_r($_FILES); print "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/91986-solved-gd-help/#findComment-471272 Share on other sites More sharing options...
schilly Posted February 20, 2008 Share Posted February 20, 2008 Second: When it does save the file, the permissions are set to 600 instead of 777. How can I fix this? Try using chmod() For the scaling I would set either the height or the width to a static value. With the static height/width values you used, you will skew the image. Try something like // Get original width and height $width = imagesx($image); $height = imagesy($image); // New width and height $new_width = 150; $new_height = $height * ($new_width / $width); Now it should be proportionate. As for saving the newly created file, I've never had to do that. I have always just created the image on the fly and sent it to the browser. Maybe create a new file with fopen then use fwrite to write the output from imagejpeg into the file. Someone else might have a better idea. Link to comment https://forums.phpfreaks.com/topic/91986-solved-gd-help/#findComment-471804 Share on other sites More sharing options...
djpic Posted February 20, 2008 Author Share Posted February 20, 2008 I have tried the chmod() command but doesn't seem to do anything. I believe I am doing it right: chmod("file.jpg",777); I know how to keep the files proportionate thanks though. I am not really worried about that right now, just want to get it to work the way I want. I guess I could have it compress the files every time they are viewed but I am sure that will be a strain on the server. Link to comment https://forums.phpfreaks.com/topic/91986-solved-gd-help/#findComment-471829 Share on other sites More sharing options...
schilly Posted February 20, 2008 Share Posted February 20, 2008 depending on where the file is located compared to where the script is executing the path name may not be correct. Maybe put an if with isfile() before it? Ya if you aren't using the original then this way is better than on the fly. Link to comment https://forums.phpfreaks.com/topic/91986-solved-gd-help/#findComment-471843 Share on other sites More sharing options...
Barand Posted February 20, 2008 Share Posted February 20, 2008 Save the newly created thumbnail with <?php imagejpeg($im, 80, $path_to_filename); // where 80 is compression quality Link to comment https://forums.phpfreaks.com/topic/91986-solved-gd-help/#findComment-472017 Share on other sites More sharing options...
djpic Posted February 20, 2008 Author Share Posted February 20, 2008 Ok, thanks Barand...that did what I wanted to do and fixed the 600 permission problem. Single line of code, perfect! Didn't know that function could do that. Anyways, here is the final code: <? // Load image $UserImage = $_FILES['userfile']['tmp_name']; $image = @imagecreatefromjpeg($UserImage); // Get original width and height $width = imagesx($image); $height = imagesy($image); // New width and height $new_width = 150; $new_height = 100; // Resample $image_resized = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); $Destination = "test/test.jpg"; imagejpeg($image_resized, $Destination, 100); ?> Link to comment https://forums.phpfreaks.com/topic/91986-solved-gd-help/#findComment-472122 Share on other sites More sharing options...
Barand Posted February 20, 2008 Share Posted February 20, 2008 That scaling algorithm is as simple as it gets, but if the user image is in portrait format instead of landscape then it going to get distorted. I prefer to set a size of the square that the thumbnail should fit into (say 150x150) then scale depending on which is larger, original height or original width Link to comment https://forums.phpfreaks.com/topic/91986-solved-gd-help/#findComment-472130 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.