【问题标题】:How to compress email attachment image on the fly with php?如何使用 php 即时压缩电子邮件附件图像?
【发布时间】:2016-08-26 12:09:36
【问题描述】:

我得到了以下使用 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 为资源,null

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

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

【问题讨论】:

标签: php image-processing gmail-imap


【解决方案1】:

好吧,我可以通过将文件写入磁盘然后压缩它并删除旧文件来解决这个问题:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    相关资源
    最近更新 更多