skilaq Posted March 14, 2008 Share Posted March 14, 2008 Hi , I am pretty new to coding in PHP and i have a requirement that i need to get done asap, be nice if one you guys could help me out. Im created a CMS where the admin user can select the page they want to upload an image to., then browse for the image and upload it to the server (specified directory). All this would be done via a form. The image is to be resized on submission to a max width of 270 and renamed to a value using time() (timestamp func) or something similar. I have found TONNES of scripts out there, which do partial jobs. Just wondering if one of you guys new about this a little more than me? or any suggested ideas. Criteria for this function is: Image can be gif, jpg, jpeg, png. Image must be uploaded to a specific directory. Image must be resized to a specific width prior to the final copy being uploaded to the server. Image to be renamed using the timestamp function to make all file names unique. Can anyone help me with this? Cheers Guys Quote Link to comment https://forums.phpfreaks.com/topic/96079-image-upload-resize-and-rename-php/ Share on other sites More sharing options...
dbillings Posted March 14, 2008 Share Posted March 14, 2008 not perfect... <?php if(isset($_REQUEST['directory'])){ $thumbarray = array(); $imagearray = array(); $dir = $_REQUEST['directory']; ini_set('memory_limit', '-1'); $jpeg_check = array(); // Checks if at least one of the files processed successfuly. if(is_dir($dir)) { if ($dh = opendir($dir)){ while (($file = readdir($dh)) !== false) { if(filetype($dir . $file) == "file") { if(exif_imagetype($dir . $file) != IMAGETYPE_JPEG) { echo $dir . $file . " Resize failed not a JPEG image. <br />"; $jpeg_check[] = 'false'; }else{ $jpeg_check[] = 'true'; $tmpname = $dir.$file; $filesize = filesize($tmpname); echo $tmpname." Type: JPEG <br />"; $orig_image = imagecreatefromjpeg($tmpname); list($width, $height) = getimagesize($tmpname); if($filesize > 157286) { // Resize image if it's over 150 kb's. $percent = 157286 / $filesize; $newwidth = $width * $percent; $newheight = $height * $percent; $new_image = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($new_image, $orig_image, 0, 0, 0, 0,$newwidth, $newheight, $width, $height); } // Generate thumbnail images. $thumbw = 50; $thumbh = $thumbw / $width * $height; $thumb = imagecreatetruecolor($thumbw, $thumbh); imagecopyresampled($thumb, $orig_image, 0, 0, 0, 0, $thumbw, $thumbh, $width, $height); $imagename = microtime().".jpg"; imagejpeg($thumb, "C:/wamp/www/thumbs/".$imagename); imagejpeg($new_image, "C:/wamp/www/images/".$imagename); } }else{ echo "$dir.$file is not a file. <br />"; } } }else{ echo "could not open $dir. <br />"; } closedir($dh); }else{ echo "The submited directory is invalid. <br />"; } $dir = "C:/wamp/www/images/"; if(is_dir($dir)) { if ($dh = opendir($dir)){ while (($file = readdir($dh)) !== false) { if(filetype($dir . $file) == "file") { $ftp_server = "ftp.yourdomain.com"; $ftp_username = "yourusername"; $ftp_user_pass = "yourpassword"; $source_file = $dir . $file; $destination_file = "images/{$file}"; $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); $login_result = ftp_login ($conn_id, $ftp_username, $ftp_user_pass); ftp_pasv($conn_id, true); if((!$conn_id || !$login_result)) { echo "FTP connection has failed."; echo "Attempted to connect to $ftp_server for $ftp_username."; exit(); }else{ echo "Connected to FTP server for $ftp_username."; } $upload = ftp_put ($conn_id, $destination_file, $source_file, FTP_BINARY); if(!$upload){ echo "FTP upload has failed."; }else{ echo "Uploaded $source_file to $ftp_server as $destination_file"; } ftp_close($conn_id); } } } }else{ echo "No JPEG images were detected for FTP upload."; } $dir = "C:/wamp/www/thumbs/"; if(is_dir($dir)) { if ($dh = opendir($dir)){ while (($file = readdir($dh)) !== false) { if(filetype($dir . $file) == "file") { $ftp_server = "ftp.yourdomain.com"; $ftp_username = "yourusername"; $ftp_user_pass = "yourpassword"; $source_file = $dir . $file; $destination_file = "thumbs/{$file}"; $conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); $login_result = ftp_login ($conn_id, $ftp_username, $ftp_user_pass); ftp_pasv($conn_id, true); if((!$conn_id || !$login_result)) { echo "FTP connection has failed."; echo "Attempted to connect to $ftp_server for $ftp_username."; exit(); }else{ echo "Connected to FTP server for $ftp_username."; } $upload = ftp_put ($conn_id, $destination_file, $source_file, FTP_BINARY); if(!$upload){ echo "FTP upload has failed."; }else{ echo "Uploaded $source_file to $ftp_server as $destination_file"; } ftp_close($conn_id); } } } }else{ echo "No JPEG thumbs were detected for FTP upload."; } } ?> <div>Format C:/upload/ </div> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> Directory<input type="text" name="directory"><br /> <input type="submit" name="submit" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/96079-image-upload-resize-and-rename-php/#findComment-491863 Share on other sites More sharing options...
dbillings Posted March 14, 2008 Share Posted March 14, 2008 BTW that script is designed to be run on a server on my computer and uploaded to my hosted webserver. PHP will not resize an image before uploading because it is executed on the webserver itself. So you will still chew up bandwidth if you upload a monsterous digital image and it will take some time. Quote Link to comment https://forums.phpfreaks.com/topic/96079-image-upload-resize-and-rename-php/#findComment-491870 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.