Posted By

onefastsnail on 05/24/12


Tagged


Versions (?)

centercrop


 / Published in: PHP
 

  1. $sizes = array(
  2. 'original' => array(
  3. 'width' => 4000,
  4. 'height' => 3000,
  5. 'destination' => '/images/stores/original/'
  6. ),
  7. 'medium' => array(
  8. 'width' => 171,
  9. 'height' => 171,
  10. 'destination' => '/images/stores/medium/',
  11. 'strict' => true
  12. ),
  13. 'small' => array(
  14. 'width' => 140,
  15. 'height' => 105,
  16. 'destination' => '/images/stores/small/'
  17. )
  18. );
  19.  
  20. function upload_image($filename, $tmp_name, $sizes){
  21. $fuploaded = array();
  22. foreach($sizes as $name => $image){
  23. if(copy($tmp_name, SITE_PATH.$image['destination'].$filename)){
  24. $size = get_image_size($image['destination'],$filename);
  25.  
  26. if($size['width'] > $image['width'] || $size['height'] > $image['height']){
  27. if($size['height'] > $size['width']){
  28. $width = $image['width'];
  29. $height = floor($size['height'] * ($image['width']/$size['width']));
  30. }
  31. else{
  32. $width = floor($size['width'] * ($image['height'] / $size['height']));
  33. $height = $image['height'];
  34. }
  35.  
  36. image_resize($width, $height, $image['destination'], $filename);
  37.  
  38. if($image['strict'] && ($width > $image['width'] || $height > $image['height'])){
  39. image_crop_centercrop($image['destination'], $filename, $width, $height, 0, 0);
  40. }
  41. }
  42. $fuploaded[]=SITE_PATH.$image['destination'].$filename;
  43. }
  44. else {
  45. echo "<br />CANT COPY!! ".SITE_PATH.$image['destination'].$filename;
  46. }
  47. }
  48. return $fuploaded;
  49. }
  50.  
  51.  
  52. if($_FILES['image']['size'] > 0) {
  53. $filename = time() . $_FILES['image']['name'];
  54. $filename = str_replace(" ", "_", $filename);
  55. $tmp_name = $_FILES['image']['tmp_name'];
  56.  
  57. $images = upload_image($filename, $tmp_name, $sizes);
  58.  
  59. print_r($images);
  60. }
  61.  
  62. function image_crop_centercrop($folder, $pic, $width, $height, $x, $y)
  63. {
  64. $command=IMAGE_MOGRIFY ." -gravity center -crop ${width}x${height}+${x}+${y} " . SITE_PATH . $folder . $pic;
  65. //echo $command;
  66. shell_exec($command);
  67. }

Report this snippet  

You need to login to post a comment.