簡體   English   中英

使用PHP旋轉然后裁剪圖像

[英]Rotate and then crop image with PHP

我正在嘗試裁剪我的圖像,但是當我這樣做時,它顯示為正確的尺寸,但全黑。 我已經嘗試了至少十幾個不同的腳本,但我似乎無法讓它們中的任何一個工作:(

哦,旋轉腳本工作正常,所有的回聲只是用於測試,將被刪除:D

<?php

$staffID        =   $_POST['u'];
$actCode        =   $_POST['a'];
$tempAvatar     =   $_POST['tempAvatar'];
$x1             =   $_POST['x'];
$y1             =   $_POST['y'];
$wH             =   $_POST['w'];
$scale          =   $_POST['scale'];
$angle          =   $_POST['angle'];
$destFolder     =   "../../../avatars/";
$imagePath      =   "tempAvatars/".$tempAvatar.".jpg";
$imagePathRot   =   "tempAvatars/".$tempAvatar."ROTATED.jpg";
$imagePathCropped=  "tempAvatars/".$tempAvatar."CROPPED.jpg";

echo 'X1: '.$x1.'<br>Y1: '.$y1.'<br>Width/Height: '.$wH.'<br>Angle: '.$angle;
if ($angle != 0) {

    $source = imagecreatefromjpeg($imagePath) or notfound();
    $rotate = imagerotate($source,$angle,0);
    imagejpeg($rotate, $imagePathRot);

    $imagePath  =   $imagePathRot;
}

echo '<br>X2: '.$x2.'<br>Y2: '.$y2;

$targ_w = 300;
$jpeg_quality = 90;

$img_r = imagecreatefromjpeg($imagePath);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_w );

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_w,$targ_h,$_POST['w'],$_POST['w']);

imagejpeg($dst_r, $imagePathCropped, $jpeg_quality);



echo '<br><img src="'.$imagePathCropped.'">';


?>

您的問題是$targ_h未定義,因此您正在從源圖像復制0像素“行”。 它的大小正確,因為它由ImageCreateTrueColor決定,當然初始化為黑色。 根據您的其余代碼進行的正確調用應該是:

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
    $targ_w,$targ_w,$_POST['w'],$_POST['w']);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM