我正在PHP中创建一个上传表单。虽然我希望表单上传一个精确的缩略图,比如100px高和100px宽度。表单必须重新缩放tumbnail而不拉伸图像,但是从图像中删除部分。我更喜欢使用100%php和最简单的脚本。我已经制作了上传表单,我只想知道如何使用我的tumbnails制作这个自动裁剪系统。
我真的希望你能帮助我,我想提前感谢你。
我的脚本已经上传了带有缩略图的图片:
if( $imgtype == 'image/jpeg' ){ $filetype= '.jpg'; }else{ $filetype= str_replace ( 'image/', '', $imgtype ); }
$path= 'images/' . md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
$thumb_path= 'images/thumb_' . md5( rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) . rand( 0, 1000 ) ) . '.jpg';
$imgsize2= getimagesize( $imgtemp );
$width= $imgsize2[0];
$height= $imgsize2[1];
$maxwidth= 1281;
$maxheight= 721;
$allowed= array( 'image/png', 'image/jpeg', 'image/gif', );
if( in_array( $imgtype, $allowed ) ){
if( $width < $maxwidth && $height < $maxheight ){
if( $imgsize < 5242880 ){
if( $width == $height ){ $case=1; }
if( $width > $height ){ $case=2; }
if( $width < $height ){ $case=3; }
switch( $case ){
case 1:
$newwidth= 100;
$newheight= 100;
break;
case 2:
$newheight= 100;
$ratio= $newheight / $height;
$newwidth= round( $width * $ratio );
break;
case 3:
$newwidth= 100;
$ratio= $newwidth / $width;
$newheight= $height * $ratio;
break;
}
switch( $imgtype ){
case 'image/jpeg';
$img= imagecreatefromjpeg( $imgtemp );
$thumb= imagecreatetruecolor( $newwidth, $newheight );
imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
imagejpeg( $thumb, $thumb_path );
break;
case 'image/png';
$img= imagecreatefrompng( $imgtemp );
$thumb= imagecreatetruecolor( $newwidth, $newheight );
imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
imagepng( $thumb, $thumb_path );
break;
case 'image/gif';
$img= imagecreatefromgif( $imgtemp );
$thumb= imagecreatetruecolor( $newwidth, $newheight );
imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
imagegif( $thumb, $thumb_path );
break;
} if(empty($errors)) {
move_uploaded_file( $imgtemp, $path );
$upimage = "Image is successfully uploaded.";
}
} else{
$errors[9] = "The image you just uploaded does not meet the requirements. Your picture is too large. ";
}
} else{
$errors[10] = "The image you just uploaded does not meet the requirements. It is a forbidden extension.";
}
} else{
$errors[11] = "The image you just uploaded does not meet the requirements. It is a forbidden extension. Type: $imgtype, $image, $imgsize, $imgtemp, $name";
}
if(empty($errors)) {
move_uploaded_file( $imgtemp, $path );
}
}
答案 0 :(得分:0)
我认为您需要将x和y坐标设置为裁剪图像。尝试这样的事情。您可能需要使用top
和left
数字,直到获得所需的数字。记下imagecopyresized
行。
这里也有一个类似的帖子,答案相同PHP - cropping image with imagecopyresampled()?
您需要设置x和y参数,如下所示。 BTW答案很容易找到。所以甚至在页面的右侧提供了相关的问题,我发现了另一个例子。干杯。
switch( $case ){
case 1:
$top = 50;
$left = 50;
$newwidth= 100;
$newheight= 100;
break;
case 2:
$newheight= 100;
$ratio= $newheight / $height;
$newwidth= round( $width * $ratio );
break;
case 3:
$newwidth= 100;
$ratio= $newwidth / $width;
$newheight= $height * $ratio;
break;
}
switch( $imgtype ){
case 'image/jpeg';
$img= imagecreatefromjpeg( $imgtemp );
$thumb= imagecreatetruecolor( $newwidth, $newheight );
imagecopyresized( $thumb, $img, 0,0,$left,$top, $newwidth, $newheight, $width, $height );
imagejpeg( $thumb, $thumb_path );
break;
case 'image/png';
$img= imagecreatefrompng( $imgtemp );
$thumb= imagecreatetruecolor( $newwidth, $newheight );
imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
imagepng( $thumb, $thumb_path );
break;
case 'image/gif';
$img= imagecreatefromgif( $imgtemp );
$thumb= imagecreatetruecolor( $newwidth, $newheight );
imagecopyresized( $thumb, $img, 0,0,0,0, $newwidth, $newheight, $width, $height );
imagegif( $thumb, $thumb_path );
break;