llamaultima Posted March 5, 2015 Share Posted March 5, 2015 Hello,I'm currently using this: http://pastebin.com/h1jMkf7J - it's working great, but I'm noticing that giant images are increasing loading times drastically, so I would like to create a thumbnail of the image as well, somewhere around 400px width and maintaining the aspect ratio. So, it'd be; uploads/theimage.png and uploads/theimage-thumbnail.png Can anyone help with this please? I've been trying different methods, such as http://www.9lessons.info/2009/03/upload-and-resize-image-with-php.html but I haven't been able to combine the two. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/295112-php-upload-image-store-it-add-mysql-entry-create-thumbnail/ Share on other sites More sharing options...
blacknight Posted April 13, 2015 Share Posted April 13, 2015 ok i have this function i wrote for you should work .... /* create thumb * * @$path path to the origional image * @$src sorce image to resize * @$name the name with out extention * @$ext the extention * @$w (default null) set the new width no required * @$h (default null) set the new height not required * !!! $width or $hight one must be set */ function _createThumb($path, $src, $name, $ext, $w=null, $h=null) { $mime = getimagesize($src); switch($mime['mime']) { case 'image/png': $src_img = imagecreatefrompng($src); break; case 'image/jpg': $src_img = imagecreatefromjpeg($src); break; case 'image/jpeg': $src_img = imagecreatefromjpeg($src); break; case 'image/pjpeg': $src_img = imagecreatefromjpeg($src); break; default : return "Unsupported picture type!"; break; } $width = imageSX($src_img); $height = imageSY($src_img); if (isset($w) && !isset($h)) { $ratio = $width/$w; } elseif(!isset($w) && isset($h)) { $ratio = $height/$h; } else { $ratio = '1'; } // allways make sure ratio is 1 or more less is bad if ($ratio < 1){$ratio = 1;} $nwidth = floor($width/$ratio); $nheight = floor($height/$ratio); $dst_img = ImageCreateTrueColor($nwidth,$nheight); imagecopyresampled($dst_img,$src_img,0,0,0,0,$nwidth,$nheight,$width,$height); $new_thumb_loc = $path.$name.'-thumbnail.'.$ext; if($mime['mime']=='image/png'){ $result = imagepng($dst_img,$new_thumb_loc,; } if($mime['mime']=='image/jpg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); } if($mime['mime']=='image/jpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); } if($mime['mime']=='image/pjpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); } imagedestroy($dst_img); imagedestroy($src_img); return true; } then use it like this notice the changes in your script $image_name = time().substr(str_replace(" ", "_", $ext), 5); $actual_image_name = $image_name.".".$ext; $tmp = $_FILES['photoimg']['tmp_name']; if(move_uploaded_file($tmp, $path.$actual_image_name)) { mysqli_query($db,"INSERT INTO `media` (title, description, image) VALUES ('$imgtitle', '$imgdesc', '$actual_image_name')"); // create thumb _createThumb($path, $path.$actual_image_name, $image_name, $ext, '400', null); // end thumb header('Location: media.php'); } Quote Link to comment https://forums.phpfreaks.com/topic/295112-php-upload-image-store-it-add-mysql-entry-create-thumbnail/#findComment-1508873 Share on other sites More sharing options...
Tom10 Posted April 13, 2015 Share Posted April 13, 2015 (edited) $mime = getimagesize($src); switch($mime['mime']) { Is unreliable Look into File Uploads <?php if(isset($_REQUEST['upload'])) { $dir = "./"; $file = $dir . basename($_FILES['userfile']['name']); $ext = pathinfo($file, PATHINFO_EXTENSION); $allowed = array('jpg', 'bmp', 'png', 'gif'); if($_FILES['userfile']['size'] > 500000) { die("File size is too large!"); } if(!in_array($ext != $allowed)) { die("The selected file is not allowed to be uploaded. You may upload files with the following extensions: jpg, png, bmp, gif"); } if(move_uploaded_file($_FILES['userfile']['tmp_name'], $file)) { echo "Your file: ".$file." has been uploaded!"; } else { echo "Upload failed"; echo "Here is some debugging information:"; print_r($_FILES); } if(is_uploaded_file($file)) { die("A file with the same name has already been uploaded. Please re-name your file and try again."); } } ?> hasn't been tested just coded it purely as an example. blacknight's code looks ok apart from mime to check the file type so you can do it which ever way just add a sql record like blacknight has when the file is uploading. Edited April 13, 2015 by Tom10 Quote Link to comment https://forums.phpfreaks.com/topic/295112-php-upload-image-store-it-add-mysql-entry-create-thumbnail/#findComment-1508927 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.