2013-04-27 123 views
-1

我调整大小并保存网址中的多个图像,并想知道如何将这些图像压缩得更多,因为保存在640x320文件夹中的图像是400kb,它太大而且想知道我如何能更多地压缩这些图像,在此先感谢您的任何建议!PHP调整图像大小并保存到服务器

PHP调整大小和SIZE HANDLER

include("../includes/picture-resize.php"); 

$image = $_POST['thumbnail']; 
$slug = $_POST['slug']; 
$images = $_POST['screenshots']; 
$list = explode(",", $images);  
$listlength = count($list); 

$i = 0; 

$image = $_POST['thumbnail']; 

$path = parse_url($image, PHP_URL_PATH); 

$filename = $slug.'-'.$i; 

$extension = pathinfo($path, PATHINFO_EXTENSION); 

$file = $filename.'.'.$extension; 

file_put_contents('../tmp/' . $file, file_get_contents($image)); 

$picture = new pic_resize(); 

$picture->load('../tmp/'.$file); 

$picture->resizeToWidth(125); 

mkdir('../images/125x125/'.$slug); 

$picture->save('../images/125x125/'.$slug.'/'.$file, $picture->image_type); 

unlink('../tmp/'.$file); 

$thumbnail = $file; 

$new_list = array(); 

mkdir('../images/640x320/'.$slug); 

mkdir('../images/310x205/'.$slug); 

while($listlength > $i) { 

    $path = parse_url($list[$i], PHP_URL_PATH); 

    $filename = $slug.'-'.$i; 

    $extension = pathinfo($path, PATHINFO_EXTENSION); 

    $file = $filename.'.'.$extension; 

    file_put_contents('../tmp/' . $file, file_get_contents($list[$i])); 

    $picture = new pic_resize(); 

    $picture->load('../tmp/'.$file); 

    $picture->resizeToWidth(640); 

    $picture->save('../images/640x320/'.$slug.'/'.$file, $picture->image_type); 

    $picture->resizeToWidth(310); 

    $picture->save('../images/310x205/'.$slug.'/'.$file, $picture->image_type); 

    unlink('../tmp/'.$file); 

    array_push($new_list, $file); 

    $i++; 
} 

PHP RESIZE CLASS

class pic_resize{ 

    var $image; 
    var $image_type; 

    function load($filename) { 
     $image_info = getimagesize($filename); 
     $this->image_type = $image_info[2]; 
     if($this->image_type == IMAGETYPE_JPEG) { 
     $this->image = imagecreatefromjpeg($filename); 
     } elseif($this->image_type == IMAGETYPE_GIF) { 
     $this->image = imagecreatefromgif($filename); 
     } elseif($this->image_type == IMAGETYPE_PNG) { 
     $this->image = imagecreatefrompng($filename); 
     } 
    } 
    function save($filename, $image_type, $compression=75, $permissions=null) { 
     if($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image,$filename,$compression); 
     } elseif($image_type == IMAGETYPE_GIF) { 
     imagegif($this->image,$filename);   
     } elseif($image_type == IMAGETYPE_PNG) { 
     imagepng($this->image,$filename,9,PNG_FILTER_PAETH); 
     } 
     if($permissions != null) { 
     chmod($filename,$permissions); 
     } 
    } 
    function output($image_type=IMAGETYPE_JPEG) { 
     if($image_type == IMAGETYPE_JPEG) { 
     imagejpeg($this->image); 
     } elseif($image_type == IMAGETYPE_GIF) { 
     imagegif($this->image);   
     } elseif($image_type == IMAGETYPE_PNG) { 
     imagepng($this->image); 
     imagealphablending($this->image, false); 
     imagesavealpha($this->image, true); 

     } 
    } 
    function getWidth() { 
     return imagesx($this->image); 
    } 
    function getHeight() { 
     return imagesy($this->image); 
    } 
    function resizeToHeight($height) { 
     $ratio = $height/$this->getHeight(); 
     $width = $this->getWidth() * $ratio; 
     $this->resize($width,$height); 
    } 
    function resizeToWidth($width) { 
     $ratio = $width/$this->getWidth(); 
     $height = $this->getheight() * $ratio; 
     $this->resize($width,$height); 
    } 
    function scale($scale) { 
     $width = $this->getWidth() * $scale/100; 
     $height = $this->getheight() * $scale/100; 
     $this->resize($width,$height); 
    } 
    function resize($width,$height) { 
     $new_image = imagecreatetruecolor($width, $height); 
     imagealphablending($new_image, false); 
     imagesavealpha($new_image, true); 

     imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 


     $this->image = $new_image; 
    }  
} 

回答

0

第三参数:
JPEG质量:压缩级别:从0(最压缩)到100(最少压缩)。
http://www.php.net/manual/en/function.imagejpeg.php

PNG质量:压缩级别:0(无压缩)到9
http://www.php.net/manual/en/function.imagepng.php

注意的是,质量/尺寸设置非常不同/ JPEG和PNG

+0

之间向后我有它设置为9,但它的文件大小是411kb,原始是620kb有没有一种方法可以压缩这更多的200kb是不是真的什么 – user2201765 2013-04-27 19:36:44