Image.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Swift Mailer Image Component
  4. * Please read the LICENSE file
  5. * @copyright Chris Corbyn <chris@w3style.co.uk>
  6. * @author Chris Corbyn <chris@w3style.co.uk>
  7. * @package Swift_Message
  8. * @license GNU Lesser General Public License
  9. */
  10. require_once dirname(__FILE__) . "/../ClassLoader.php";
  11. Swift_ClassLoader::load("Swift_Message_EmbeddedFile");
  12. /**
  13. * Embedded Image component for Swift Mailer
  14. * @package Swift_Message
  15. * @author Chris Corbyn <chris@w3style.co.uk>
  16. */
  17. class Swift_Message_Image extends Swift_Message_EmbeddedFile
  18. {
  19. /**
  20. * Constructor
  21. * @param Swift_File The input source file
  22. * @param string The filename to use, optional
  23. * @param string The MIME type to use, optional
  24. * @param string The Content-ID to use, optional
  25. * @param string The encoding format to use, optional
  26. */
  27. public function __construct(Swift_File $data=null, $name=null, $type="application/octet-stream", $cid=null, $encoding="base64")
  28. {
  29. parent::__construct($data, $name, $type, $cid, $encoding);
  30. }
  31. /**
  32. * Set data for the image
  33. * This overrides setData() in Swift_Message_Attachment
  34. * @param Swift_File The data to set, as a file
  35. * @throws Swift_Message_MimeException If the image cannot be used, or the file is not
  36. */
  37. public function setData($data, $read_filename=true)
  38. {
  39. if (!($data instanceof Swift_File)) throw new Exception("Parameter 1 of " . __METHOD__ . " must be instance of Swift_File");
  40. parent::setData($data, $read_filename);
  41. $img_data = @getimagesize($data->getPath());
  42. if (!$img_data)
  43. {
  44. throw new Swift_Message_MimeException(
  45. "Cannot use file '" . $data->getPath() . "' as image since getimagesize() was unable to detect a file format. " .
  46. "Try using Swift_Message_EmbeddedFile instead");
  47. }
  48. $type = image_type_to_mime_type($img_data[2]);
  49. $this->setContentType($type);
  50. if (!$this->getFileName()) $this->setFileName($data->getFileName());
  51. }
  52. }