我試圖在PHP中將圖像從一個正方形裁剪成一個圓。我在網上看到很多解決方案,通過掩蓋初始圖像並使角落變成不同的顏色來完成獲取圓形圖像。但是,這是有問題的,因爲將角設置爲透明僅僅會導致我的初始方形圖像。例如,下面的代碼的結果的圓形圖像與粉紅色的角落在沒有蒙版的情況下在PHP中裁剪圖像
$image_name = $_POST['filepath'];
$source_image = imagecreatefrompng($image_name);
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = $_POST['width'];
$dest_imagey = $_POST['height'];
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagealphablending($dest_image, true);
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
header("Content-Type: image/jpeg");
// create masking
$mask = imagecreatetruecolor($source_imagex, $source_imagey);
$mask = imagecreatetruecolor($dest_imagex, $dest_imagey);
$pink = imagecolorallocate($mask, 255, 0, 255);
imagefill($mask, 0, 0, $pink);
$black = imagecolorallocate($mask, 0, 0, 0);
imagecolortransparent($mask, $black);
imagefilledellipse($mask, $dest_imagex/2, $dest_imagey/2, $dest_imagex, $dest_imagey, $black);
imagecopy($dest_image, $mask, 0, 0, 0, 0, $dest_imagex, $dest_imagey);
imagecolortransparent($dest_image, $pink);
imagejpeg($dest_image, NULL);
有一種方法以裁剪,使得邊緣被實際刪除在PHP的圖像?
這有什麼錯用CSS爲了這? '邊界半徑:50%'應該達到你所需要的。 – BenM 2014-10-02 22:41:06
我與iOS接口,所以我只需要返回圓形圖像 – user3781236 2014-10-02 22:45:13
然後有什麼問題:'cell.yourImageView.layer.cornerRadius = cell.yourImageView.frame.size.height/2; cell.yourImageView.layer.masksToBounds = YES; cell.yourImageView.layer.borderWidth = 0;'? – BenM 2014-10-02 22:46:12