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