2012-01-19 68 views
2

我需要先按比例調整圖像大小(寬度是重要尺寸),然後裁剪任何多餘的高度,然後將新版本存儲在目錄中。在GD庫中調整大小後裁剪圖像

我已經設法調整大小的罰款,我最終與我的目錄中正確的寬度的圖像。有些人在這裏需要裁剪多餘的高度。但我無法弄清楚我需要做什麼。我是否需要以某種方式使用copyimageresampled。我想裁剪所有圖像,使其高度爲50像素。

這裏是我迄今爲止FO調整大小:

$src = ImageCreateFromJpeg($upfile); 
$dst = ImageCreateTrueColor($tn_width, $tn_height); 
ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height); 
ImageJpeg($dst, 'images/' . $_FILES['image']['name']); 

回答

-1

Croping就像與調整GD

一些示例代碼:

// Original image 
$filename = 'someimage.jpg'; 

// Get dimensions of the original image 
list($current_width, $current_height) = getimagesize($filename); 

// The x and y coordinates on the original image where we 
// will begin cropping the image 
$left = 50; 
$top = 50; 

// This will be the final size of the image (e.g. how many pixels 
// left and down we will be going) 
$crop_width = 200; 
$crop_height = 200; 

// Resample the image 
$canvas = imagecreatetruecolor($crop_width, $crop_height); 
$current_image = imagecreatefromjpeg($filename); 
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height); 
imagejpeg($canvas, $filename, 100); 

定義您的作物寬度&高度,應該全部設定。它只不過是一個調整大小,你可以看到。

參考:http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/

+0

除非我錯看了你的回覆我之後有點微妙的不同。我不想放棄全尺寸的圖像。我想先按照正確的寬度按比例調整大小,然後從調整大小的圖像中切掉多餘的高度。我認爲上面的代碼只是裁剪一個全尺寸的圖像? –

+0

@elduderino,你正在描述一個作物,不管你是否調整大小,作物定義了'新'圖像的尺寸,並切斷了其他部分(高度/寬度),只需從'$ left'和'$頂部座標,(0和0),並定義裁剪的最終尺寸(比如說200x200) – Jakub

+0

我知道我正在描述裁剪,但我需要先將圖像調整到正確的寬度。然後,由此產生的圖像,我需要砍掉任何多餘的高度。裁剪全尺寸的圖片對我來說不起作用。我需要找到一種方法來使用我上面的代碼,並在調整大小階段後合併裁剪,我已經在做。我似乎無法得到正確的代碼。 –

0

這可以幫助你調整大小後裁剪:911-need-code-help

<?php 
    //---------------------------------------------------------------- 
    // Crop-to-fit PHP-GD 
    // Revision 2 [2009-06-01] 
    // Corrected aspect ratio of the output image 
    //---------------------------------------------------------------- 

    define('DESIRED_IMAGE_WIDTH', 150); 
    define('DESIRED_IMAGE_HEIGHT', 150); 

    $source_path = $_FILES[ 'Image1' ][ 'tmp_name' ]; 

    // 
    // Add file validation code here 
    // 

    list($source_width, $source_height, $source_type) = getimagesize($source_path); 

    switch ($source_type) 
    { 
    case IMAGETYPE_GIF: 
     $source_gdim = imagecreatefromgif($source_path); 
     break; 

    case IMAGETYPE_JPEG: 
     $source_gdim = imagecreatefromjpeg($source_path); 
     break; 

    case IMAGETYPE_PNG: 
     $source_gdim = imagecreatefrompng($source_path); 
     break; 
    } 

    $source_aspect_ratio = $source_width/$source_height; 
    $desired_aspect_ratio = DESIRED_IMAGE_WIDTH/DESIRED_IMAGE_HEIGHT; 

    if ($source_aspect_ratio > $desired_aspect_ratio) 
    { 
    // 
    // Triggered when source image is wider 
    // 
    $temp_height = DESIRED_IMAGE_HEIGHT; 
    $temp_width = (int) (DESIRED_IMAGE_HEIGHT * $source_aspect_ratio); 
    } 
    else 
    { 
    // 
    // Triggered otherwise (i.e. source image is similar or taller) 
    // 
    $temp_width = DESIRED_IMAGE_WIDTH; 
    $temp_height = (int) (DESIRED_IMAGE_WIDTH/$source_aspect_ratio); 
    } 

    // 
    // Resize the image into a temporary GD image 
    // 

    $temp_gdim = imagecreatetruecolor($temp_width, $temp_height); 
    imagecopyresampled(
    $temp_gdim, 
    $source_gdim, 
    0, 0, 
    0, 0, 
    $temp_width, $temp_height, 
    $source_width, $source_height 
); 

    // 
    // Copy cropped region from temporary image into the desired GD image 
    // 

    $x0 = ($temp_width - DESIRED_IMAGE_WIDTH)/2; 
    $y0 = ($temp_height - DESIRED_IMAGE_HEIGHT)/2; 

    $desired_gdim = imagecreatetruecolor(DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT); 
    imagecopy(
    $desired_gdim, 
    $temp_gdim, 
    0, 0, 
    $x0, $y0, 
    DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT 
); 

    // 
    // Render the image 
    // Alternatively, you can save the image in file-system or database 
    // 

    header('Content-type: image/jpeg'); 
    imagejpeg($desired_gdim); 

    // 
    // Add clean-up code here 
    // 
?>