如何使用php动态压缩电子邮件附件图像?

时间:2016-05-01 18:32:22

标签: php image-processing gmail-imap

我有以下脚本使用php imap库下载电子邮件附件(图片):

$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()期望参数1为resource,null

    imagejpeg($image, $destination, $quality);

通常这适用于具有物理位置的图像。由于这里的图像在内存中,我似乎无法在将它们写入物理驱动器之前对其进行压缩。

1 个答案:

答案 0 :(得分:0)

好吧,我有点能够通过将文件写入磁盘然后压缩它并删除旧文件来解决问题:

            $img = $email . "-" . $filename;
            $fp = fopen($img, "w+");
            fwrite($fp, $attachment['attachment']);               
            fclose($fp);

            compressImage($img, $filename, 50);
            unlink($img);

这不是理想的解决方案,因为它会进行不必要的写入。我很想得到一些反馈!