从上传的多个图像调整大小相同的比例PHP
问题描述:
我一直在砸我的头几个小时,我不能为我的生活弄清楚如何调整一个简单的形式上传相同比例的图像。如果有人上传比2048px水平或垂直更大的图像,我想将其大小调整为2048px的大小,然后将其保存在文件夹中。从上传的多个图像调整大小相同的比例PHP
由于我基本恢复,只有我的形式我没有什么给你看,但遗憾的是大多已被搜索和阅读GD但对我不起作用划伤......
任何提示都非常不胜感激!
编辑:
if(isset($con, $_POST['save_button'])){
// IMAGE PROCESSING
$name = $_FILES['file_upload']['name'];
$tmp_name = $_FILES['file_upload']['tmp_name'];
$type = $_FILES['file_upload']['type'];
$size = $_FILES['file_upload']['size'];
$error = $_FILES['file_upload']['error'];
move_uploaded_file($tmp_name, "social_images/$name.jpg");
function resize_image($img, $w, $h, $crop=FALSE) {
list($width, $height) = getimagesize($img);
$r = $width/$height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$src = imagecreatefromjpeg($img);
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $dst;
}
$img = resize_image("social_images/$name.jpg", 780, 780);
header("location: index.php");
exit();
}
答
新鲜重试并重新测试:
<?php
function shazam($file, $w, $h) {
list($width, $height) = getimagesize($file);
if ($width > $height) {
$r = ($w/$width);
$newwidth = $w;
$newheight = ceil($height * $r);
}
if ($width < $height) {
$r = ($h/$height);
$newheight = $h;
$newwidth = ceil($width * $r);
}
if ($width == $height) {
$newheight = $h;
$newwidth = $w;
}
$src = imagecreatefromjpeg($file);
$tgt = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tgt, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $tgt;
}
$img = shazam("thepicwithpath.jpg", 850, 850);
imagejpeg($img, "theresizedpicwithpath.jpg", 75);
?>
注意,imagejpeg()的到底是什么东西实际上产生新的文件。
这里是其余的:https://stackoverflow.com/questions/14649645/resize-image-in-php – deg
谢谢你的快速回复!我已经尝试了一下,但仍然没有得到它的诀窍。你能看看代码吗?编辑帖子 – Nilsson1188
无后顾之忧。请查看已知工作示例的答案(公平地说,我只用景观测试过,但我相当有信心)。 – deg