我正在尝试裁剪和调整图像的大小。当图像移动到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);
}
}
?>
发布于 2016-02-01 15:19:13
将代码行更改为:
$pic = $_FILES["slideshow_images"]["tmp_name"];
$image = imagecreatefromstring(file_get_contents(($pic_src)));因为name只有123.jpg,所以它不是object。
最好的方法是:
<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';发布于 2016-02-01 15:10:42
我刚刚遇到了这个问题。问题是背景颜色是黑色的,而且完全透明。你要做的就是分配一个真实的颜色(如白色)给alpha,并使alpha完全不透明。然后,只需先在新区域上做一个填充矩形,然后您的图像就会显示出来。:-)
以下内容直接摘自php文档中关于imagecopy的内容:
// 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);请注意,在实际复制新图像之前,他们使用背景颜色进行了imagefill。对于imagecopyresample也是如此。
和以前不同的是,这一次我没有得到黑色的图像。因此,根据以下内容检查您所做的工作:实际上,下载以下内容并运行它(以及test.jpg映像)。看看它对你是否有效。请注意,这篇文章直接来自于imagecopyresample的PHP文档网站。
<?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);
?>下面是图片:

下面是输出:

https://stackoverflow.com/questions/35124709
复制相似问题