php - 在 php 中从中心自动裁剪 Tumble 的照片

标签 php mysql image

我需要在 php 中从照片中心自动裁剪图像。我怎样才能做到这一点?首先,我上传原始图像大小,然后我想裁剪它并保存在目录中。

这是我的代码:

<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>

上传代码:

<?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')");

?>

我想裁剪并将其上传到 $folder = "gallery_photo/thumbs" 上,并且还想插入到 mysql 数据库中。

最佳答案

上传原始图像后裁剪图像并传递图像位置, 目的地位置和您的预期宽度,高度将根据您的宽度计算。

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);
}

代码引用:Link

或者您可以引用这篇文章:Creating a thumbnail from an uploaded image

关于php - 在 php 中从中心自动裁剪 Tumble 的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44420376/

相关文章:

php - 表单中的选项输入未通过

javascript - 阻止电话访问子域M

mysql - Rails Mysql2::错误表不存在创建新迁移时

php - 为什么一个 php 标签内的两个查询不起作用

image - 对图像进行机器学习时减少特征数量的方法

css - 多个边框,带填充,围绕图像

php - 如何与 PHP 一起高效检索 Mysql 表的前 20 个引用值

mysql - 错误 1064 : SQL syntax

html - 背景图像不会随着 Firefox 中的屏幕大小而缩小

php - PHP 中的字符串有多大?