也許是因爲你不是使用正確的功能 ...
$CI->image_lib->resize();
你不是說crop()
?我不想說這個,但是RTM。
這裏是一個可以裁剪,同時調整功能:
function ImageCR($source, $crop = null, $scale = null, $destination = null)
{
$source = @ImageCreateFromString(@file_get_contents($source));
if (is_resource($source) === true)
{
$size = array(ImageSX($source), ImageSY($source));
if (isset($crop) === true)
{
$crop = array_filter(explode('/', $crop), 'is_numeric');
if (count($crop) == 2)
{
$crop = array($size[0]/$size[1], $crop[0]/$crop[1]);
if ($crop[0] > $crop[1])
{
$size[0] = $size[1] * $crop[1];
}
else if ($crop[0] < $crop[1])
{
$size[1] = $size[0]/$crop[1];
}
$crop = array(ImageSX($source) - $size[0], ImageSY($source) - $size[1]);
}
else
{
$crop = array(0, 0);
}
}
else
{
$crop = array(0, 0);
}
if (isset($scale) === true)
{
$scale = array_filter(explode('*', $scale), 'is_numeric');
if (count($scale) >= 1)
{
if (empty($scale[0]) === true)
{
$scale[0] = $scale[1] * $size[0]/$size[1];
}
else if (empty($scale[1]) === true)
{
$scale[1] = $scale[0] * $size[1]/$size[0];
}
}
else
{
$scale = array($size[0], $size[1]);
}
}
else
{
$scale = array($size[0], $size[1]);
}
$result = ImageCreateTrueColor($scale[0], $scale[1]);
if (is_resource($result) === true)
{
ImageFill($result, 0, 0, IMG_COLOR_TRANSPARENT);
ImageSaveAlpha($result, true);
ImageAlphaBlending($result, true);
if (ImageCopyResampled($result, $source, 0, 0, $crop[0]/2, $crop[1]/2, $scale[0], $scale[1], $size[0], $size[1]) === true)
{
if (preg_match('~gif$~i', $destination) >= 1)
{
return ImageGIF($result, $destination);
}
else if (preg_match('~png$~i', $destination) >= 1)
{
return ImagePNG($result, $destination, 9);
}
else if (preg_match('~jpe?g$~i', $destination) >= 1)
{
return ImageJPEG($result, $destination, 90);
}
}
}
}
return false;
}
使用方法如下:
// resize to 400x400 px
ImageCR('path/to/sourceImg.jpg', null, '400*400', 'path/to/outputImg.jpg');
// crop to a 1:1 ratio (square) from the center
ImageCR('path/to/sourceImg.jpg', '1/1', null, 'path/to/outputImg.jpg');
// crop to a 1:1 ratio (square) from the center AND resize to 400x400 px
ImageCR('path/to/sourceImg.jpg', '1/1', '400*400', 'path/to/outputImg.jpg');
// crop to a 1:1 ratio (square) from the center AND resize to 400 px width AND maintain aspect ratio
ImageCR('path/to/sourceImg.jpg', '1/1', '400*', 'path/to/outputImg.jpg');
// crop to a 1:1 ratio (square) from the center AND resize to 400 px height AND maintain aspect ratio
ImageCR('path/to/sourceImg.jpg', '1/1', '*400', 'path/to/outputImg.jpg');
播放角落找尋與選項,如果你有任何疑問只是讓我知道。
對不起我的兄弟......,有一些錯誤的。 第一,我想用我的coodinate裁剪圖像。 x,y,x1,y1。 之後,我想使用我的寬度和高度來調整圖像大小。 因爲我的結果是拇指圖像不是裁剪圖像,所以我使用調整大小功能。 你能幫我嗎,在這裏我必須把x1,y2的配置? 謝謝 – 2009-12-24 06:36:54
當然有一些錯誤,請再次閱讀您的問題,並嘗試理解您對它的要求。其次,'x,y,x1,y1,x1,y2' **什麼?!**您只需要4個變量來裁剪圖像:'x1,x2,y1,y2'我猜你的'x1'和'y1'是'$ x'和'$ y',你的'x2'和'y2'實際上是'$ width'和'$ height',但就像我說的那樣 - **我只是猜測**你不是很清楚。再次嘗試使用'crop()'方法並閱讀手冊。 – 2009-12-24 07:17:19
我已經閱讀手冊man ...,這個詞必須是x1,y1配置 它不需要x2和y2。因爲我輸入x,y,x1,y1。 我檢查了image_lib代碼,庫不支持裁剪和同時調整大小。 所以我必須做一些修改image_process_gd();功能 – 2009-12-25 09:25:16