Php 旋转和裁剪图像批处理
编辑:请忘记这个问题!我在原始代码中犯了一个愚蠢的错误。 示例代码按预期工作 我正在尝试旋转和裁剪图像。 到目前为止,我有:Php 旋转和裁剪图像批处理,php,Php,编辑:请忘记这个问题!我在原始代码中犯了一个愚蠢的错误。 示例代码按预期工作 我正在尝试旋转和裁剪图像。 到目前为止,我有: $w = 100; $h = 400; $img1 = imagecreatefromjpeg('image1.jpg'); $img2 = imagecreatefromjpeg('image2.jpg'); for ($i = 0; $i < 2; $i++) { ${'img'.$i + 3} = imagecreatetruecolor($h, $h
$w = 100;
$h = 400;
$img1 = imagecreatefromjpeg('image1.jpg');
$img2 = imagecreatefromjpeg('image2.jpg');
for ($i = 0; $i < 2; $i++) {
${'img'.$i + 3} = imagecreatetruecolor($h, $h);
imagecopy(${'img'.$i + 3}, ${'img'.$i + 1}, 0, 0, 0, 0, $w, $h);
${'img'.$i + 3} = imagerotate(${'img'.$i + 3}, 90, 0);
${'img'.$i + 3} = imagecrop(${'img'.$i + 3}, array(0, 0, $h, $w));
imagejpeg(${'img'.$i + 3});
imagedestroy(${'img'.$i + 3});
imagedestroy(${'img'.$i + 1});
}
$w=100;
$h=400;
$img1=imagecreatefromjpeg('image1.jpg');
$img2=imagecreatefromjpeg('image2.jpg');
对于($i=0;$i<2;$i++){
${'img'.$i+3}=ImageCreateTureColor($h,$h);
图像复制(${'img'.$i+3},${'img'.$i+1},0,0,0,$w,$h);
${'img'.$i+3}=imagerotate(${'img'.$i+3},90,0);
${'img'.$i+3}=imagecrop(${'img'.$i+3},数组(0,0,$h,$w));
图像JPEG(${'img'.$i+3});
imagedestroy(${'img'.$i+3});
imagedestroy(${'img'.$i+1});
}
所以我要做的就是打开一些JPG,创建新图像,将JPG复制到新图像中,然后裁剪图像
唉,这会导致空图像
我做错了什么?不知道这是否会对缺少输出产生任何影响-但是
$img1
&$img2
做了什么-就我所知,它们没有被使用
@error_reporting( E_ALL );
$w = 100;
$h = 400;
$img1 = imagecreatefromjpeg('image1.jpg');
$img2 = imagecreatefromjpeg('image2.jpg');
for ($i = 0; $i < 3; $i++) {
$new=${'img'.$i + 3};
$src=${'img'.$i + 1};
$new = imagecreatetruecolor($h, $h);
imagecopy( $new, $src, 0, 0, 0, 0, $w, $h);
$new = imagerotate($new, 90, 0);
$new = imagecrop($new, array(0, 0, $h, $w));
imagejpeg($new);
imagedestroy($new);
imagedestroy($src);
break;/* just to see if it gets this far*/
}
@错误报告(E_ALL);
$w=100;
$h=400;
$img1=imagecreatefromjpeg('image1.jpg');
$img2=imagecreatefromjpeg('image2.jpg');
对于($i=0;$i<3;$i++){
$new=${'img'.$i+3};
$src=${'img'.$i+1};
$new=ImageCreateTureColor($h,$h);
imagecopy($new,$src,0,0,0,$w,$h);
$new=imagerotate($new,90,0);
$new=imagecrop($new,数组(0,0,$h,$w));
图像JPEG(新);
(新的);
(1)(src);
休息;/*看看能不能走这么远*/
}
不知道这是否会对缺少输出产生任何影响-但是$img1
&$img2
做了什么-据我所知,它们没有被使用
@error_reporting( E_ALL );
$w = 100;
$h = 400;
$img1 = imagecreatefromjpeg('image1.jpg');
$img2 = imagecreatefromjpeg('image2.jpg');
for ($i = 0; $i < 3; $i++) {
$new=${'img'.$i + 3};
$src=${'img'.$i + 1};
$new = imagecreatetruecolor($h, $h);
imagecopy( $new, $src, 0, 0, 0, 0, $w, $h);
$new = imagerotate($new, 90, 0);
$new = imagecrop($new, array(0, 0, $h, $w));
imagejpeg($new);
imagedestroy($new);
imagedestroy($src);
break;/* just to see if it gets this far*/
}
@错误报告(E_ALL);
$w=100;
$h=400;
$img1=imagecreatefromjpeg('image1.jpg');
$img2=imagecreatefromjpeg('image2.jpg');
对于($i=0;$i<3;$i++){
$new=${'img'.$i+3};
$src=${'img'.$i+1};
$new=ImageCreateTureColor($h,$h);
imagecopy($new,$src,0,0,0,$w,$h);
$new=imagerotate($new,90,0);
$new=imagecrop($new,数组(0,0,$h,$w));
图像JPEG(新);
(新的);
(1)(src);
休息;/*看看能不能走这么远*/
}
>P>首先,您应该考虑使用更好的过程质量。
然后,您需要正确使用该函数,目前您只需加载JPG并直接输出它们,这可能是您想要的,但您处于循环中,无法将多个图像直接循环加载到同一个文件。这也意味着你看到(或没有)的图像是集合中的最终图像
您的问题是,您的for
循环运行了三次,但您只有与前两个实例关联的数据,但第三个实例是空的,因为这是最近的一个实例,所以这是唯一输出到浏览器的实例
因此:
1) 保存使用imagejpeg($data,$filename)生成的图像代码>。您可以将文件名定义为$filename=$img+3。“.jpg”代码>或类似
2) 如果使用数组而不是数字递增的变量,那么调试和读取代码也会容易得多,这是一种非常混乱的代码编写方式
3) 如果您确实希望将图像直接输出到浏览器,则需要PHP提供标题,例如header('Content-Type:image/jpeg')在输出imagejpeg
的内容之前执行code>
代码的重述:
$w = 100;
$h = 400;
$img[1] = imagecreatefromjpeg('image1.jpg');
$img[2] = imagecreatefromjpeg('image2.jpg');
for ($i = 3; $i < 5; $i++) {
$img[$i] = imagecreatetruecolor($h, $h);
$j = $i -2;
imagecopyresampled($img[$i], $img[$j], 0, 0, 0, 0, $h, $h, $w, $h,);
// this function also contains destination width and destination
// height, which are equal to the size of the destination image so
// are set as $h, $h in the function above.
$img[$i] = imagerotate($img[$i], 90, 0);
$img[$i] = imagecrop($img[$i], array(0, 0, $h, $w));
$filename = "image-".$i.".jpg";
imagejpeg($img[$i], $filename);
imagedestroy($img[$i]);
imagedestroy($img[$j]);
}
$w=100;
$h=400;
$img[1]=imagecreatefromjpeg('image1.jpg');
$img[2]=imagecreatefromjpeg('image2.jpg');
对于($i=3;$i<5;$i++){
$img[$i]=ImageCreateTureColor($h,$h);
$j=$i-2;
imagecopyresampled($img[$i],$img[$j],0,0,0,0,$h,$h,$w,$h,);
//此函数还包含目标宽度和目标宽度
//高度,等于目标图像的大小,因此
//在上述函数中设置为$h,$h。
$img[$i]=imagerotate($img[$i],90,0);
$img[$i]=imagecrop($img[$i],数组(0,0,$h,$w));
$filename=“image-”$i.“.jpg”;
图像JPEG($img[$i],$filename);
图像销毁($img[$i]);
图像销毁($img[$j]);
}
>P>首先,您应该考虑使用更好的过程质量。
然后,您需要正确使用该函数,目前您只需加载JPG并直接输出它们,这可能是您想要的,但您处于循环中,无法将多个图像直接循环加载到同一个文件。这也意味着你看到(或没有)的图像是集合中的最终图像
您的问题是,您的for
循环运行了三次,但您只有与前两个实例关联的数据,但第三个实例是空的,因为这是最近的一个实例,所以这是唯一输出到浏览器的实例
因此:
1) 保存使用imagejpeg($data,$filename)生成的图像代码>。您可以将文件名定义为$filename=$img+3。“.jpg”代码>或类似
2) 如果使用数组而不是数字递增的变量,那么调试和读取代码也会容易得多,这是一种非常混乱的代码编写方式
3) 如果您确实希望将图像直接输出到浏览器,则需要PHP提供标题,例如header('Content-Type:image/jpeg')在输出imagejpeg
的内容之前执行code>
代码的重述:
$w = 100;
$h = 400;
$img[1] = imagecreatefromjpeg('image1.jpg');
$img[2] = imagecreatefromjpeg('image2.jpg');
for ($i = 3; $i < 5; $i++) {
$img[$i] = imagecreatetruecolor($h, $h);
$j = $i -2;
imagecopyresampled($img[$i], $img[$j], 0, 0, 0, 0, $h, $h, $w, $h,);
// this function also contains destination width and destination
// height, which are equal to the size of the destination image so
// are set as $h, $h in the function above.
$img[$i] = imagerotate($img[$i], 90, 0);
$img[$i] = imagecrop($img[$i], array(0, 0, $h, $w));
$filename = "image-".$i.".jpg";
imagejpeg($img[$i], $filename);
imagedestroy($img[$i]);
imagedestroy($img[$j]);
}
$w=100;
$h=400;
$img[1]=imagecreatefromjpeg('image1.jpg');
$img[2]=imagecreatefromjpeg('image2.jpg');
对于($i=3;$i<5;$i++){
$img[$i]=ImageCreateTureColor($h,$h);
$j=$i-2;
imagecopyresampled($img[$i],$img[$j],0,0,0,0,$h,$h,$w,$h,);
//此函数还包含目标宽度和目标宽度
//高度,它们是相等的