gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:49:35 25 4
gpt4 key购买 nike

我需要在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com