2016-05-01 17 views
0

ich habe folgende Skripte, die PHP-imap-Bibliothek verwendet E-Mail-Anhänge (Bilder) zum Download:Wie komprimiere ich E-Mail-Anhang Bild mit 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; 
} 

Wenn ich diesen Code, den ich den Fehler

fehlgeschlagen bekommen laufen Stream zu öffnen: Datei oder das Verzeichnis

$info = getimagesize($source); 

imagejpeg() erwartet Parameter 1 bis b e Ressource, null

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

Normalerweise funktioniert dies für Bilder, die physischen Standort haben. Da die Bilder hier im Speicher sind, kann ich nicht scheinen, sie zu komprimieren, bevor ich sie auf das physische Laufwerk schreibe.

+0

Mögliches Duplikat von [Fehler beim Öffnen des Streams: Keine solche Datei oder kein Verzeichnis] ​​(http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) –

Antwort

0

Nun, ich war irgendwie in der Lage, um das Problem zu erhalten, indem Sie die Datei auf die Festplatte geschrieben und komprimiert mich dann und Entfernen der alten:

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

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

Dies ist nicht die ideale Lösung, da es unnötig macht schreiben. Ich würde gerne etwas Feedback bekommen!