Showing posts with label tumb create in php. Show all posts
Showing posts with label tumb create in php. Show all posts

Friday 4 August 2017

Creating a thumbnail from an uploaded image using php

Creating a thumbnail from an uploaded image using php


<?php

function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){
    $imgsize = getimagesize($source_file);
    $width = $imgsize[0];
    $height = $imgsize[1];
    $mime = $imgsize['mime'];

    switch($mime){
        case 'image/gif':
            $image_create = "imagecreatefromgif";
            $image = "imagegif";
            break;

        case 'image/png':
            $image_create = "imagecreatefrompng";
            $image = "imagepng";
            $quality = 100;
            break;

        case 'image/jpeg':
            $image_create = "imagecreatefromjpeg";
            $image = "imagejpeg";
            $quality = 100;
            break;

        default:
            return false;
            break;
    }
    
    $dst_img = imagecreatetruecolor($max_width, $max_height);
    $src_img = $image_create($source_file);
    
    $width_new = $height * $max_width / $max_height;
    $height_new = $width * $max_height / $max_width;
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
    if($width_new > $width){
        //cut point by height
        $h_point = (($height - $height_new) / 2);
        //copy image
       // imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
    }else{
        //cut point by width

        $w_point = (($width - $width_new) / 2);
  
       // imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
    }
    
    imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $max_width, $max_height, $width, $height);
    $image($dst_img, $dst_dir);


    if($dst_img)imagedestroy($dst_img);
    if($src_img)imagedestroy($src_img);

$imagedata =ob_end_clean();
return $dst_dir;

}

$img = $_FILES['file']['tmp_name'];
  $file_name = "aaa/"."thumb_".$_FILES['file']['name'];
resize_crop_image(250, 250,$img, $file_name);

?>

<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="save" value="save"/>

</form>