我需要在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数据库中插入。
答案 0 :(得分:0)
上传原始图片后裁剪图片并传递图片位置, 目的地宽度和您的预期宽度,高度将根据您的宽度计算。
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