0
私は電子メールの添付ファイル(画像)をダウンロードするPHPのIMAPライブラリを使用して、次のスクリプトました:どのようにPHPで電子メールの添付ファイルのイメージをオンザフライで圧縮するには?
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to mail server: ' . imap_last_error());
foreach($emails as $email) {
/* get mail structure */
$structure = imap_fetchstructure($inbox, $email);
$attachments = array();
/* if any attachments found... */
if(isset($structure->parts) && count($structure->parts))
{
for($i = 0; $i < count($structure->parts); $i++)
{
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if($structure->parts[$i]->ifdparameters)
{
foreach($structure->parts[$i]->dparameters as $object)
{
if(strtolower($object->attribute) == 'filename')
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters)
{
foreach($structure->parts[$i]->parameters as $object)
{
if(strtolower($object->attribute) == 'name')
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if($attachments[$i]['is_attachment'])
{
$attachments[$i]['attachment'] = imap_fetchbody($inbox, $email, $i+1);
/* 4 = QUOTED-PRINTABLE encoding */
if($structure->parts[$i]->encoding == 3)
{
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
/* 3 = BASE64 encoding */
elseif($structure->parts[$i]->encoding == 4)
{
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
/* iterate through each attachment and save it */
foreach($attachments as $attachment)
{
if($attachment['is_attachment'] == 1)
{
$filename = $attachment['name'];
if(empty($filename)) $filename = $attachment['filename'];
if(empty($filename)) $filename = time() . ".dat";
/* prefix the email number to the filename in case two emails
* have the attachment with the same file name.
*/
$source_img = $attachment['attachment'];
$dest_img = 'lowres.jpg';
$fp = fopen($email . "-" . $filename, "w+");
fwrite($fp, compressImage($source_img, $dest_img, 75));
fclose($fp);
}
}
}
//Image Compression
function compressImage($source, $destination, $quality){
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return $destination;
}
私はこのコードを実行すると、私はエラー
を取得するには、ストリームをオープンに失敗しました:いいえそのようなファイルをまたはディレクトリ
$info = getimagesize($source);
imagejpeg()Bにパラメータ1を期待
imagejpeg($image, $destination, $quality);
ヌル電子リソースは、通常、これは物理的な場所を持っていたイメージのために働きます。ここの画像はメモリに保存されているので、物理的なドライブに書き込む前に圧縮することはできません。
[ストリームを開くことに失敗しました:そのようなファイルまたはディレクトリはありません](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) –