[英]PHP GD imagecopyresampled() and flip it horizontal
我正在使用 imagecopyresampled() 從另一個 PNG 圖像渲染 PNG 圖像。 現在我希望圖像的某些部分水平翻轉,所以我試過這個:
//horizontal
$src_x = $width - 1;
$src_width = -$width;
imagecopyresampled(
$imgdest, $imgsrc, 0, 0, $src_x, $src_y , $width, $height
, $src_width, $src_height
);
取自PHP 手冊的用戶評論。
它在我的情況下不起作用(我從原始圖像復制了很多片段到新圖像),而是復制了另一幅圖像。 有沒有人有解決方案?
我知道這有點晚了,但我自己也在尋找這個解決方案,只是找到了所需的代碼......
function image_flip($img, $type=''){
$width = imagesx($img);
$height = imagesy($img);
$dest = imagecreatetruecolor($width, $height);
switch($type){
case '':
return $img;
break;
case 'vert':
for($i=0;$i<$height;$i++){
imagecopy($dest, $img, 0, ($height - $i - 1), 0, $i, $width, 1);
}
break;
case 'horiz':
for($i=0;$i<$width;$i++){
imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);
}
break;
case 'both':
for($i=0;$i<$width;$i++){
imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);
}
$buffer = imagecreatetruecolor($width, 1);
for($i=0;$i<($height/2);$i++){
imagecopy($buffer, $dest, 0, 0, 0, ($height - $i -1), $width, 1);
imagecopy($dest, $dest, 0, ($height - $i - 1), 0, $i, $width, 1);
imagecopy($dest, $buffer, 0, $i, 0, 0, $width, 1);
}
imagedestroy($buffer);
break;
}
return $dest;
}
好吧,在我自己找到答案多年之后,我只想讓其他人知道。
這很簡單,例如:
代替:
imagecopy($output, $input, 8, 20, 4, 20, 4, 12)
我會用:
imagecopyresampled($output, $input, 8, 20, (8 - 1), 20, 4, 12, 0 - 4, 12);
這將水平翻轉圖像的一部分。
我用那個:
imageflip ( resource $image , int $mode ) : bool
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.