0

I got the following scripts that uses php imap library to download email attachments (images):

$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;
}

When I run this code I get the error

failed to open stream: No such file or directory

    $info = getimagesize($source);

imagejpeg() expects parameter 1 to be resource, null

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

Usually this works for images that have physical location. Since the images here are in the memory, I can't seem to compress them before writing them to physical drive.

thethakuri
  • 509
  • 5
  • 16
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew May 05 '16 at 19:12

1 Answers1

0

Well, I was sort of able to get around the problem by writing the file to the disk and then compressing it and removing the old one :

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

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

This is not the ideal solution as it does unnecessary write. I would love to get some feedback !

thethakuri
  • 509
  • 5
  • 16