Posted By

simsevu on 09/20/10


Tagged


Versions (?)

image to server


 / Published in: PHP
 

  1. function insert_image($inserted_product, $product_id){
  2.  
  3. $image_url = $inserted_product["url"];
  4. $image_types = array("jpg", "jpeg", "png", "gif", "bmp");
  5.  
  6. $file_extension = end(explode(".", $inserted_product["image"]));
  7.  
  8. switch($file_extension){
  9. case "jpeg":
  10. $image_source = imagecreatefromjpeg($image_url);
  11. break;
  12. case "jpg":
  13. $image_source = imagecreatefromjpeg($image_url);
  14. break;
  15. case "gif":
  16. $image_source = imagecreatefromgif($image_url);
  17. break;
  18. case "png":
  19. $image_source = imagecreatefrompng($image_url);
  20. break;
  21. case "bmp":
  22. $image_source = imagecreate($image_url);
  23. break;
  24. }
  25.  
  26. $color_compression = 65;
  27.  
  28. $bigger_image_size = getimagesize($image_source);
  29. $org_width = $bigger_image_size[0];
  30. $org_height = $bigger_image_size[1];
  31.  
  32. $widthSmall = 75;
  33. $heightSmall = ($widthSmall / $org_width) * $org_height;
  34.  
  35. $widthBigger = 250;
  36. $heightBigger = ($widthBigger / $org_width) * $org_height;
  37.  
  38. $dir = '../shops/'.$inserted_product["shop_id"].'/products/'.$product_id.'/';
  39. create_folder($dir);
  40.  
  41. $small_path = $dir.$inserted_product["slug"].'_lille.jpg';
  42. $bigger_path = $dir.$inserted_product["slug"].'_stor.jpg';
  43.  
  44. $small_image = imagecreatetruecolor($widthSmall, $heightSmall);
  45. $bigger_image = imagecreatetruecolor($widthBigger, $heightBigger);
  46.  
  47. imagecopyresampled($small_image, $image_source, 0, 0, 0, 0, $widthSmall, $heightSmall, $org_width, $org_height);
  48. imagecopyresized($bigger_image, $image_source, 0, 0, 0, 0, $widthBigger, $heightBigger, $org_width, $org_height);
  49.  
  50. imagejpeg($small_image, $small_path, $color_compression);
  51. imagejpeg($bigger_image, $bigger_path, $color_compression);
  52. }
  53.  
  54. function create_folder($path) {
  55. $structure = explode("/", $path);
  56. $lastPath = '';
  57.  
  58. foreach ($structure as $folder) {
  59. if ($lastPath) {
  60. $folder = $lastPath.'/'.$folder;
  61. }
  62.  
  63. if (!is_dir($folder)) {
  64. mkdir($folder);
  65. chmod($folder, 0755);
  66. }
  67.  
  68. $lastPath = $folder;
  69. }
  70. }

Report this snippet  

You need to login to post a comment.