簡體   English   中英

PHP - 使用imagecopyresampled()裁剪圖像?

[英]PHP - cropping image with imagecopyresampled()?

我想使用imagecreatetruecolor裁剪圖像,它總是裁剪它留下黑色空間,或者縮放太大。 我希望圖像精確到191px寬和90px高,所以我還需要調整圖像大小以及裁剪,因為必須保持比例。 那么,有一些項目的樣本:

在此輸入圖像描述

調整大小腳本(簡化)如下所示:

$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 ['crop']數組包括:

['x'] => $_POST['inp-x']
['y'] => $_POST['inp-x']
['width'] => $_POST['inp-width']
['height'] => $_POST['inp-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函數,它允許您傳入具有xywidthheight值的數組。

好吧,我自己發現了問題,代碼應該是這樣的:

imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['newWidth'], 191, 90, $newImage['crop']['height']); 

暫無
暫無

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

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