Posted By

jkochis on 07/16/06


Tagged

php image gd cakephp


Versions (?)

Image Manager Componet


 / Published in: PHP
 

makes a resized duplicate of an image

  1. example:
  2. $imgId = $_FILES['article_image']['name'];
  3. //make thumbnail
  4. $this->ImageManager->makeImage(WWW_ROOT.'/img/articleImages/'.$imgId, WWW_ROOT.'/img/articleThumbs/'.$imgId, 85, 110);
  5.  
  6.  
  7. <?php
  8. class ImageManagerComponent extends Object
  9. {
  10. var $controller;
  11.  
  12. function makeImage($input, $output, $max_width, $max_height)
  13. {
  14. $im = imagecreatefromjpeg($input);
  15. $orig_height = imagesy($im);
  16. $orig_width = imagesx($im);
  17. $ratio = ($orig_height>$orig_width) ? $max_width/$orig_width : $max_height/$orig_height;
  18. $new_width = $orig_width * $ratio;
  19. $new_height = $orig_height * $ratio;
  20. $new_im = imagecreatetruecolor($new_width,$new_height);
  21. imagecopyresampled($new_im,$im,0,0,0,0,$new_width,$new_height,$orig_width,$orig_height);
  22. imagejpeg($new_im);
  23. $image = ob_get_clean();
  24. $thumb_pointer = fopen($output,'w+');
  25. fputs($thumb_pointer,$image,strlen($image));
  26. fclose($thumb_pointer);
  27. }
  28.  
  29. function delete($fileName)
  30. {
  31. if (unlink($fileName))
  32. {
  33. return true;
  34. }
  35. else
  36. {
  37. return false;
  38. }
  39. }
  40. }
  41. ?>

Report this snippet  

You need to login to post a comment.