PHP imagecopyresampled leaves empty are on image after cropping - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Wednesday, September 27, 2023

PHP imagecopyresampled leaves empty are on image after cropping

I try to write a custom function for resizing and resizing and cropping images. I use several samples i found from this forum. It is almost as i want it.. But when i resize and crop the image it leaves empty unwanted area:

enter image description here

The first image is the original image (size is restricted) and the second is resized and cropped. As you can see by the purple frame around the pictures there are some leftover on the resized picture and i can't find the why to remove it. In this case the resized and cropped image needs to be exactly square (as the visible part is).

My code is here:

function resize_image($file_source, $file_destination, $w, $h, $crop = true, $crop_after_resize = false) 
{
    if(!is_file($file_source)) {
        
        die('Not file: ' . $file_source);
    }
    
    #$image_type = pathinfo($file_source);
    #$image_type = $image_type['extension'];
    
    $image_type = mime_content_type($file_source);
    $image_type = explode('/', $image_type)[1];
    $image_type = strtolower($image_type);
    
    $allowed_extensions = [
        'jpg',
        'jpeg',
        'png',
        'gif',
        'bmp',
    ];
    
    if(!in_array($image_type, $allowed_extensions)) {
        
        die('Extension not allowed: ' . $image_type);
    }
    
    list($width, $height) = getimagesize($file_source);
    $r = $width / $height;
    
    if ($crop) {
        
        if(($width / $w) < ($height / $h)) {
            $ratio = $width / $w;
            $newwidth = $w;
            $newheight = round($height / $ratio, 0);
            
        } else {
            $ratio = $height / $h;
            $newheight = $h;
            $newwidth = round($width / $ratio);
        }
        
    } else {
        
        if ($w/$h > $r) {
        
            $newwidth = $h * $r;
            $newheight = $h;
        
        } else {
        
            $newheight = $w/$r;
            $newwidth = $w;
        }
    }
    
    $src = false;
    
    if($image_type == 'jpg' || $image_type == 'jpeg') {
        
        $src = imagecreatefromjpeg($file_source);
    
    } elseif($image_type == 'png') {
        
        $src = imagecreatefrompng($file_source);
    
    } elseif($image_type == 'gif') {
        
        $src = imagecreatefromgif($file_source);
    
    } elseif($image_type == 'bmp') {
        
        $src = imagecreatefrombmp($file_source);
    
    }
    
    if(!$src) {
        pr($file_source);
        pr($file_destination);
        pr('is file: ' . is_file($file_source));
        pr('crop: ' . $crop);
        pr('crop after resize: ' . $crop_after_resize);
        pr($file_source);
        pr($image_type);
        pr(mime_content_type($file_source));
        pr(var_dump($src), 1);
    }
    
    $image = imagecreatetruecolor(round($newwidth, 0), round($newheight, 0));
    
    $white = imagecolorallocate($image, 255, 255, 255); 
    imagefill($image,0,0,$white); 
    
    if(!$crop_after_resize) {
        
        imagecopyresampled($image, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    }
    
    if($crop_after_resize) {
        
        $padding_left = ($width - $w) / 2;
        $padding_top = ($height - $h) / 2;
        
        pr('width: ' . $width);
        pr('height: ' . $height);
        pr('------------------------------------');
        pr('desired width: ' . $w);
        pr('desired height: ' . $h);
        pr('------------------------------------');
        pr('new width: ' . $newwidth);
        pr('new height: ' . $newheight);
        pr('------------------------------------');
        pr('padding left: ' . $padding_left);
        pr('padding top: ' . $padding_top, 1);
        
        if(!$src) {
            
            return false;
        }
        
        # imagecopyresampled($image, $src, 0, 0, round($padding_left, 0), round($padding_top, 0), $w, $h, $w, $h);
        imagecopy($image, $src, 0, 0, round($padding_left, 0), round($padding_top, 0), $w, $h);
        
        generateImageByResourceAndFiletype($image, $image_type, $file_destination);
    
    } else {
        
        generateImageByResourceAndFiletype($image, $image_type, $file_destination);
        
        if($crop) {
            
            resize_image($file_destination, $file_destination, $w, $h, true, true);
        }
    }
    
}

function generateImageByResourceAndFiletype($image, $image_type, $file_destination) 
{
    if($image_type == 'jpg' || 'jpeg') {
        
        $result = imagejpeg($image, $file_destination);
    
    } elseif($image_type == 'png') {
        
        $result = imagepng($image, $file_destination);
    
    } elseif($image_type == 'gif') {
        
        $result = imagegif($image, $file_destination);
    
    } elseif($image_type == 'bmp') {
        
        $result = imagebmp($image, $file_destination);
    }
    
    return $result;
}



# $filename_source = 'test1.png';
# $filename_destination = 'thumb_test1.png';

$filename_source = 'Agape_2019_07_28.jpg';
$filename_destination = 'thumb_Agape_2019_07_28.jpg';

$folder = __DIR__ . '/template/images/';
$file_source = $folder . $filename_source;
$file_destination = $folder . $filename_destination;

#$result = resize_image($file_source, $file_destination, 160, 256, false);

resize_image($file_source, $file_destination, 393, 393, true);
#resize_image($file_destination, $file_destination, 393, 393, true);

function pr($content) 
{
    echo '<pre>';
    print_r($content);
    echo '</pre>';
}

?>

<img src="/template/images/<?php echo $filename_source; ?>" style="max-width: 500px; border: 2px solid #FF00FF;" alt="" />
<img src="/template/images/<?php echo $filename_destination; ?>" style="border: 2px solid #FF00FF;" alt="" />

I hope you can help and point out where is my mistake or how can i fix this.. Thank you in advance



source https://stackoverflow.com/questions/77182115/php-imagecopyresampled-leaves-empty-are-on-image-after-cropping

No comments:

Post a Comment