我正在尝试裁剪并调整图像大小。当图像移动到resized_images文件夹时,所有图像都会变为黑色,但图像会调整大小(535 * 313)。这是我到目前为止尝试过的代码。能否请你建议我这样做的正确方法。谢谢你
<form action="" method="POST" enctype="multipart/form-data">
<input id="input-6" name="slideshow_images[]" type="file" multiple class="file-loading">
<input type="submit" name="sub" >
</form>
<?php
if(isset($_POST['sub']))
{
$pic = $_FILES["slideshow_images"]["name"];
foreach($pic as $pic_src)
{
$image = imagecreatefromjpeg($pic_src);
$filename = 'resized_images/'.$pic_src.'cropped_whatever.jpeg';
$thumb_width = 535;
$thumb_height = 313;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
}
}
?>
答案 0 :(得分:1)
将您的代码行更改为:
$pic = $_FILES["slideshow_images"]["tmp_name"];
$image = imagecreatefromstring(file_get_contents(($pic_src)));
因为[“name”]只是123.jpg,所以不是对象。
最好的方法是:
<form action="" method="POST" enctype="multipart/form-data">
<input id="input-6" name="slideshow_images[]" type="file" multiple class="file-loading">
<input type="submit" name="sub" >
</form>
<?php
if(isset($_POST['sub'])){
if(isset($_FILES['slideshow_images'])){
foreach ($_FILES["slideshow_images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["slideshow_images"]["tmp_name"][$key];
$name = $_FILES["slideshow_images"]["name"][$key];
$image = imagecreatefromstring(file_get_contents(($tmp_name)));
$filename = 'images/'.$name.'cropped_whatever.jpg';
$thumb_width = 535;
$thumb_height = 313;
$width = imagesx($image);
$height = imagesy($image);
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = $width / ($height / $thumb_height);
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $height / ($width / $thumb_width);
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
0 - ($new_height - $thumb_height) / 2, // Center the image vertically
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, $filename, 80);
}
}
}
}
?>
此外,如果您不想在文件名中添加“.jpg”,请将$ filename行替换为:
$filename = 'images/'.preg_replace('/\.[^.]*$/', '', $name).'cropped_whatever.jpg';
答案 1 :(得分:0)
我刚遇到这个问题。问题是背景颜色是黑色还是完全透明。你要做的就是用alpha分配一个真正的颜色(如白色)并使alpha完全不透明。然后,首先在新区域上填充矩形,然后显示图像。 : - )
以下是关于imagecopy的php文档:
// create new image with padding
$img = imagecreatetruecolor($right-$left+$padding*2,$bottom-$top+$padding*2);
// Allocate background color
$white = imagecolorallocatealpha( $img, 255, 255, 255, 0 );
// fill the background
imagefill($img, 0, 0, $white);
// or use
imagefilledrectangle( $img, 0,0,$width,$height, $white );
// copy
imagecopy($img, $image, $padding, $padding, $left, $top, $right-$left, $bottom-$top);
请注意,在实际复制新图像之前,他们会使用背景颜色进行图像填充。 imagecopyresample也是如此。
嗯,不像以前 - 这次我没有得到黑色图像。因此,请检查您对以下内容执行的操作:实际上,请下载并运行它(以及test.jpg图像)。看看它是否适合你。请注意,这直接来自PHP文档网站for imagecopyresample。
<?php
// The file
$filename = './test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, "new.jpg", 100);
?>
这是图像: