Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.

Commit fd05bed

Browse files
author
Martin Damien
authored
Merge pull request #19 from n3o77/master
check if image file exists
2 parents b86c6f1 + 9cd37c5 commit fd05bed

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

src/ImageEmbedPlugin.php

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,57 @@ protected function embedImages(Swift_Mime_SimpleMessage $message, Swift_Mime_Sim
7171
* see https://github.com/swiftmailer/swiftmailer/issues/139
7272
*/
7373
if (strpos($src, 'cid:') === false) {
74+
$path = $this->getPathFromSrc($src);
7475

75-
$path = $src;
76+
if ($this->fileExists($path)) {
77+
$entity = \Swift_Image::fromPath($path);
78+
$message->setChildren(
79+
array_merge($message->getChildren(), [$entity])
80+
);
7681

77-
if (filter_var($src, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) === false) {
78-
$path = $this->basePath . $src;
82+
$image->setAttribute('src', 'cid:' . $entity->getId());
7983
}
80-
81-
$entity = \Swift_Image::fromPath($path);
82-
$message->setChildren(
83-
array_merge($message->getChildren(), [$entity])
84-
);
85-
86-
$image->setAttribute('src', 'cid:' . $entity->getId());
8784
}
8885
}
8986

9087
return $dom->saveHTML();
9188
}
89+
90+
/**
91+
* @param $path
92+
*
93+
* @return bool
94+
*/
95+
protected function isUrl($path)
96+
{
97+
return filter_var($path, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED) !== false;
98+
}
99+
100+
/**
101+
* @param $src
102+
*
103+
* @return string
104+
*/
105+
protected function getPathFromSrc($src)
106+
{
107+
if ($this->isUrl($src)) {
108+
return $src;
109+
}
110+
111+
return $this->basePath . $src;
112+
}
113+
114+
/**
115+
* @param $path
116+
*
117+
* @return bool
118+
*/
119+
protected function fileExists($path)
120+
{
121+
if ($this->isUrl($path)) {
122+
return !!@getimagesize($path);
123+
}
124+
125+
return file_exists($path);
126+
}
92127
}

0 commit comments

Comments
 (0)