簡體   English   中英

按大小壓縮圖像

[英]Compress an image by its size

是否可以使用PHP按尺寸壓縮圖像? 我的意思是,我讓用戶輸入圖像的大小(以KB為單位),然后他們得到該大小的圖像。

您可以通過使用新尺寸重新創建圖像來壓縮圖像。

$image_name = $_FILES['image']['name'];
$image_name_tmp = $_FILES['image']['tmp_name'];
$ext = pathinfo($image_name, PATHINFO_EXTENSION);

if($ext=='jpg' || $ext=='jpeg' || $ext=='JPG' || $ext=='JPEG')
{
    $src = imagecreatefromjpeg($image_name_tmp);
}
else if($ext=="png" || $ext=="PNG")
{
$src = imagecreatefrompng($image_name_tmp);
}
else 
{
$src = imagecreatefromgif($image_name_tmp);
}


list($width, $height) = getimagesize($image_name_tmp);

$n_width = 320;
$n_height = ($height / $width) * $n_width;

$temp_image = imagecreatetruecolor($n_width, $n_height);
imagecopyresampled($temp_image, $src, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
imagejpeg($temp_image, $target, 100);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM