0

I need auto crop image from the center of photo in php. How can I do this? First I upload the orginal image size and than I want to crop it and save on a directory.

Here is my code:

<form name="photo" id="photo" method="post" action="photo_ac.php" enctype="multipart/form-data">
                                    <div class="form-group">
                                      <input type="file" id="photo_upload" name="photo_upload" class="form-control sa_file">
                                    </div>
                                    <div class="sn_warning">

                                        <p>
                                            By clicking 'Upload photo' you confirm that you have permission from the copyright holder. 
                                            <a href="#">Having problems uploading photos?</a>
                                        </p>

                                    </div>
                                    <div class="save_cancl">

                                        <button class="btn btn-primary sv_cn_btn" type="submit">Upload Photo</button> 

                                    </div>
                                </form>

Upload Code :

<?php

// IMAGE UPLOAD ///////////////////////
    $folder = "gallery_photo/";
    $extention = strrchr($_FILES['photo_upload']['name'], ".");
    $new_name = time();
    $photo_upload = $new_name.$extention;
    $uploaddir = $folder . $photo_upload;
    move_uploaded_file($_FILES['photo_upload']['tmp_name'], $uploaddir);
//////////////////////////////////////////////////



    //$insert_action = mysqli_query($con,"INSERT INTO `gallery_photos` (`id`, `user_id`, `photo_upload`) VALUES (NULL, '$user_id', '$photo_upload')");

?>

I want to crop and upload it on $folder = "gallery_photo/thumbs" and also want to insert at mysql database.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

Crop your image after uploading your original image and pass your image location, destination loation and your expected width, Height will calculate based on your width.

function create_thumb($src, $dest, $desired_width) {

    /* read the source image */
    $source_image = imagecreatefromjpeg($src);
    $width = imagesx($source_image);
    $height = imagesy($source_image);

    /* find the "desired height" of this thumbnail, relative to the desired width  */
    $desired_height = floor($height * ($desired_width / $width));

    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

    /* copy source image at a resized size */
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

    /* create the physical thumbnail image to its destination */
    imagejpeg($virtual_image, $dest, 100);
}

Code Ref : Link

Or your can refer this post : Creating a thumbnail from an uploaded image

Santosh
  • 393
  • 2
  • 11