Leao Posted August 23, 2006 Share Posted August 23, 2006 Hi, I'm using a form to upload a jpeg image to my server via PHP (see code). I'd like to rename the jpeg as title.jpg before uploading it to the server and also I'd like to reject the image if it isn't exactly 500x375 pixels.I've searched the net but can only find a few tutorials about renaming files with random file names in order to avoid overwriting old ones but I actually want to overwrite old image files so I want to rename files to a specific name rather than to a random one. I can't find much either about checking an image's pixel dimensions either.Thanks – leao[code]<?php$target = "upload/";$target = $target . basename( $_FILES['uploaded']['name']);$ok=1;if ($uploaded_size > 70000){echo "Your file is too large.<br>";$ok=0;}if (!($uploaded_type=="image/jpeg")) {echo "You may only upload JPEG files.<br>";$ok=0;}if ($ok==0){Echo "Sorry your file was not uploaded";}else{if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded";}else{echo "Sorry, there was a problem uploading your file.";}}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18470-checking-uploaded-images-pixel-dimensions/ Share on other sites More sharing options...
jvalarta Posted August 23, 2006 Share Posted August 23, 2006 I built something about a couple years ago that did this exact thing. here's a some bits and pieces. If you need more, let me know.[code]// GET IMAGE ATTRIBUTESlist($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($import);// CHECK IMAGE MIME TYPE TO ENSURE IT'S A JPEGif ($ImageMimeType != 2) { echo "<FONT FACE=\"verdana,arial\" SIZE=\"1\"><B>Upload Error:</B> This image does not appear to be JPG format."; if ($ImageMimeType == 1) {$WrongType = 'GIF';} if ($ImageMimeType == 3) {$WrongType = 'PNG';} if ($ImageMimeType == 4) {$WrongType = 'SWF';} if ($ImageMimeType == 5) {$WrongType = 'PSD';} if ($ImageMimeType == 6) {$WrongType = 'BMP';} if ($ImageMimeType == 7 || $ImageMimeType == 8) {$WrongType = 'TIFF';} if ($ImageMimeType > 8) {$WrongType = 'UNKNOWN FORMAT';} echo "<P>It appears to be a $WrongType. Please upload JPG format only."; echo "<BR><BR><BR><BR><CENTER><INPUT TYPE=\"button\" VALUE=\"Close\" ONCLICK=\"window.close()\"></CENTER>"; unlink($import); exit;}// CHECK FILE WIDTH and HEIGHT FOR ACCURACYif ($ImportWidth > $ImportHeight) { if ($ImportWidth != 1125 && $ImportHeight != 675) { echo "<FONT FACE=\"verdana,arial\" SIZE=\"1\"><B>Upload Error:</B> The image you are uploading is not the proper size."; echo "<P>It appears to be $ImportWidth x $ImportHeight."; echo "<P>It should be 1125 x 675 for a horizontal card at 300dpi. <P>Please correct and re-upload.</FONT>"; echo "<BR><BR><BR><BR><CENTER><INPUT TYPE=\"button\" VALUE=\"Close\" ONCLICK=\"window.close()\"></CENTER>"; unlink($import); exit; }}else { if ($ImportWidth != 675 && $ImportHeight != 1125) { echo "<FONT FACE=\"verdana,arial\" SIZE=\"1\"><B>Upload Error:</B> The image you are uploading is not the proper size."; echo "<P>It appears to be $ImportWidth x $ImportHeight."; echo "<P>It should be 1125 x 675 for a vertical card at 300dpi.<P>Please correct and re-upload.</FONT>"; echo "<BR><BR><BR><BR><CENTER><INPUT TYPE=\"button\" VALUE=\"Close\" ONCLICK=\"window.close()\"></CENTER>"; unlink($import); exit; }}[/code]You will need GD installed, of course. As for renaming the upload file, here you go:[code]//copy the file to permanent locationcopy($import, "path/to/images/$VarName.jpg");[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18470-checking-uploaded-images-pixel-dimensions/#findComment-79512 Share on other sites More sharing options...
Leao Posted August 23, 2006 Author Share Posted August 23, 2006 The file is renamed as it's uploaded now, thanks!I'm new to PHP so I'm afraid your code for checking the image's pixel dimensions is perplexing. Is there a way of checking an image's pixel dimensions in a similar way to how I check the image's file size below, using getimagesize() for example?[code=php:0]if (!($uploaded_type=="image/jpeg")) {echo "You may only upload JPEG files.<br>";$ok=0;}if ($ok==0){Echo "Sorry your file was not uploaded";}[/code]Cheers,Leao Quote Link to comment https://forums.phpfreaks.com/topic/18470-checking-uploaded-images-pixel-dimensions/#findComment-79532 Share on other sites More sharing options...
AndyB Posted August 23, 2006 Share Posted August 23, 2006 [quote]Is there a way of checking an image's pixel dimensions in a similar way to how I check the image's file size below, using getimagesize() for example?[/quote]Yup. The first line of code posted earlier:[code]list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($filename);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18470-checking-uploaded-images-pixel-dimensions/#findComment-79534 Share on other sites More sharing options...
litebearer Posted August 23, 2006 Share Posted August 23, 2006 A little cavet, you mentioned a specific size (height and width). If you set both parameters, it will most likely result in an image that is somewhat distorted.Just in case, here is a little ditty that should be able to handle the majority if not all of what you are looking to accomplish.http://www.nstoia.com/toh/technical/imageresize/index.phpLite... Quote Link to comment https://forums.phpfreaks.com/topic/18470-checking-uploaded-images-pixel-dimensions/#findComment-79538 Share on other sites More sharing options...
Leao Posted August 23, 2006 Author Share Posted August 23, 2006 I don't really want to resize images, just to check if they're the correct dimensions and then ask the user to reupload the images at the right dimensions if they're not.I tried the getimagesize() function but it didn't work. Even if the image was exactly 500 pixels in width I still got the 'Your file is not the correct width...' error message and also: [quote]PHP Warning: getimagesize(image.jpg): failed to open stream: No such file or directory in C:\hshome\mydomain\mydomain.org\test\upload.php on line 7[/quote]The php I tried was:[code=php:0]list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($_FILES['uploaded']['name']) ; if ($ImportWidth != 500){echo "Your file is not the correct width of 500 pixels.<br>";$ok=0;}[/code]I probably entered it incorrectly.thanx leo Quote Link to comment https://forums.phpfreaks.com/topic/18470-checking-uploaded-images-pixel-dimensions/#findComment-79564 Share on other sites More sharing options...
AndyB Posted August 23, 2006 Share Posted August 23, 2006 if ($[b]ImportWidth[/b] != 500) Quote Link to comment https://forums.phpfreaks.com/topic/18470-checking-uploaded-images-pixel-dimensions/#findComment-79601 Share on other sites More sharing options...
Leao Posted August 23, 2006 Author Share Posted August 23, 2006 Hi, thanks. When I copied and pasted the code to PHP Freaks I changed the variables to suit those you had used but forgot to change one of them. It doesn't work even with the proper variables.Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/18470-checking-uploaded-images-pixel-dimensions/#findComment-79608 Share on other sites More sharing options...
hitman6003 Posted August 23, 2006 Share Posted August 23, 2006 If you want it to be smaller than 500 px, wouldn't it be:[code]if ($ImportWidth >= 500)[/code]and the list line should be:[code]list($ImportWidth,$ImportHeight,$ImageMimeType) = getimagesize($_FILES['uploaded']['tmp_name']) ;[/code]not $_FILES['uploaded']['name']. Quote Link to comment https://forums.phpfreaks.com/topic/18470-checking-uploaded-images-pixel-dimensions/#findComment-79611 Share on other sites More sharing options...
Leao Posted August 24, 2006 Author Share Posted August 24, 2006 Thank you very much : )It works great! Quote Link to comment https://forums.phpfreaks.com/topic/18470-checking-uploaded-images-pixel-dimensions/#findComment-79637 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.