Create Image thumb while uploading


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. if(!empty($_FILES)){
  2. $img = explode('.', $_FILES['thumb_img']['name']);
  3. $img_path = $img_dir .$img[0].time().'.'.$img[1];
  4. $img_thumb = $img_dir .$img[0].time().'_thumb.'.$img[1];
  5. $extension = strtolower($img[1]);
  6. if(in_array($extension , array('jpg','jpeg', 'gif', 'png', 'bmp'))){
  7.  
  8. //---------- To create thumbnail of image---------------
  9. if($extension=="jpg" || $extension=="jpeg" ){
  10. $src = imagecreatefromjpeg($_FILES['thumb_img']['tmp_name']);
  11. }
  12. else if($extension=="png"){
  13. $src = imagecreatefrompng($_FILES['thumb_img']['tmp_name']);
  14. }
  15. else{
  16. $src = imagecreatefromgif($_FILES['thumb_img']['tmp_name']);
  17. }
  18. list($width,$height)=getimagesize($_FILES['thumb_img']['tmp_name']);
  19. $newwidth=151;
  20. $newheight=151;//($height/$width)*$newwidth;
  21. $tmp=imagecreatetruecolor($newwidth,$newheight);
  22. imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight, $width,$height);
  23. imagejpeg($tmp,$img_thumb,100);
  24. //-------------- Ends -----------
  25.  
  26. move_uploaded_file($_FILES['thumb_img']['tmp_name'], $img_path);
  27. $blog_thumb = get_site_option('rt_blog_thumb');
  28. $blog_thumb[$blog_id] = basename($img_thumb);
  29. update_site_option('rt_blog_thumb', $blog_thumb);
  30. }
  31. else echo 'Invalid file';
  32. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.