علم و فلسفه
علم و فلسفه راهی برای شناخت خود و هستی
یک سال پیش / خواندن دقیقه

تغییر اندازه عکس در php - resize image in php

برای تغییر اندازه jpg jpeg میتونید از کد زیر استفاده کنید

Resize JPEG Format Image

Resize JPEG image.

function resize_image_jpeg($source_file,$destination_file, $width, $height, $quality, $crop=FALSE) {
    list($current_width, $current_height) = getimagesize($source_file);
    $rate = $current_width / $current_height;
    if ($crop) {
        if ($current_width > $current_height) {
            $current_width = ceil($current_width-($current_width*abs($rate-$width/$height)));
        } else {
            $current_height = ceil($current_height-($current_height*abs($rate-$width/$height)));
        }
        $newwidth = $width;
        $newheight = $height;
    } else {
        if ($width/$height > $rate) {
            $newwidth = $height*$rate;
            $newheight = $height;
        } else {
            $newheight = $width/$rate;
            $newwidth = $width;
        }
    }
    $src_file = imagecreatefromjpeg($source_file);
    $dst_file = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst_file, $src_file, 0, 0, 0, 0, $newwidth, $newheight, $current_width, $current_height);
    imagejpeg($dst_file, $destination_file, $quality);
}

چطور از تابع استفاده کنیم : 

resize_image_jpeg('image.jpg','dst_image.jpg','100','100',75,false);

Resize PNG Format Image

PNG images are slightly different from JPEG format due to the possibility of being transparent. The way to determine the width and height is the same as the previous function and the only difference is the way to create and save the image.

function resize_image_png($source_file,$destination_file, $width, $height, $quality, $crop=FALSE) {
    list($current_width, $current_height) = getimagesize($source_file);
    $rate = $current_width / $current_height;
    if ($crop) {
        if ($current_width > $current_height) {
            $current_width = ceil($current_width-($current_width*abs($rate-$width/$height)));
        } else {
            $current_height = ceil($current_height-($current_height*abs($rate-$width/$height)));
        }
        $newwidth = $width;
        $newheight = $height;
    } else {
        if ($width/$height > $rate) {
            $newwidth = $height*$rate;
            $newheight = $height;
        } else {
            $newheight = $width/$rate;
            $newwidth = $width;
        }
    }
    $src_file = imagecreatefrompng($source_file);
    imagepalettetotruecolor($src_file);
    imagealphablending($src_file, true);
    imagesavealpha($src_file, true);
    $dst_file = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst_file, $src_file, 0, 0, 0, 0, $newwidth, $newheight, $current_width, $current_height);
    imagepng($dst_file, $destination_file, $quality);
}

طریقه استفاده

resize_image_png('image.png','dst_image.png','100','100',7,false);


شاید از نوشته‌های زیر خوشتان بیاید
نظر خود را درباره این پست بنویسید ...

منوی سریع