English 中文(简体)
PHP-用imagecopyresoampled()裁剪图像?
原标题:PHP - cropping image with imagecopyresampled()?

我想使用imagecreatetruecolor裁剪图像,但它总是在裁剪时留下黑色空间,或者缩放太大。我希望图像正好是191像素宽和90像素高,所以我还需要调整图像的大小,以及裁剪,因为必须保持比例。以下是该项目的一些示例:

调整大小脚本(简化)如下所示:

$src_img=imagecreatefromjpeg($photoTemp);    
list($width,$height)=getimagesize($photoTemp);
$dst_img=imagecreatetruecolor(191, 90);
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage[ crop ][ x ], $newImage[ crop ][ y ], $newImage[ crop ][ width ], $newImage[ crop ][ height ], $width, $height);

$newImage[cropt]数组包括:

[ x ] => $_POST[ inp-x ]
[ y ] => $_POST[ inp-x ]
[ width ] => $_POST[ inp-width ]
[ height ] => $_POST[ inp-height ]

但我得到的是:

有人看到了吗?我做错了什么?

谢谢,迈克。

最佳回答

好的,我自己发现了问题,代码应该是这样的:

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage[ crop ][ x ], $newImage[ crop ][ y ], $newImage[ newWidth ], 191, 90, $newImage[ crop ][ height ]); 
问题回答

你也可以做,我自己也做了,而且效果很好

(x1,y1)=>;从哪里开始收割

(x2,y2)=>;作物的终点

$filename = $_GET[ imageurl ];
$percent = 0.5;

list($width, $height) = getimagesize($filename);

$new_width  = $_GET[ x2 ] - $_GET[ x1 ];
$new_height = $_GET[ y2 ] - $_GET[ y1 ];

$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);

imagecopyresampled($image_p, $image, 0, 0 , $_GET[ x1 ] , $_GET[ y1 ] , $new_width, $new_height, $new_width, $new_height);

// Outputs the image
header( Content-Type: image/jpeg );
imagejpeg($image_p, null, 100);

尝试

<?php

$dst_img = imagecreatetruecolor($newImage[ crop ][ width ], $newImage[ crop ][ height ]);

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage[ crop ][ x ], $newImage[ crop ][ y ], 0, 0, $width, $height);

还有imagecrop函数,该函数允许您传入具有xywidth值的数组。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签