/**
  * Test {@link crop_to_constraint()}
  */
 function test_crop_to_constraint()
 {
     // 50x50 => 20x20
     $this->assertEqual(crop_to_constraint(50, 50, 20, 20), array(0, 0, 50, 50));
 }
Exemple #2
0
/**
 * Generate a thumbnail
 *
 * @param resource Image resource
 * @param string Thumbnail type ('crop'|'fit')
 * @param int Thumbnail width
 * @param int Thumbnail height
 * @param int Thumbnail percent of blur effect (0 - No blur, 1% - Max blur effect, 99% - Min blur effect)
 * @return array short error code + dest image handler
 */
function generate_thumb($src_imh, $thumb_type, $thumb_width, $thumb_height, $thumb_percent_blur = 0)
{
    $src_width = imagesx($src_imh);
    $src_height = imagesy($src_imh);
    if ($src_width <= $thumb_width && $src_height <= $thumb_height) {
        // There is no need to resample, use original!
        return array(NULL, $src_imh);
    }
    switch ($thumb_type) {
        case 'crop':
        case 'crop-top':
            $align = $thumb_type == 'crop-top' ? 'top' : 'center';
            list($src_x, $src_y, $src_width, $src_height) = crop_to_constraint($src_width, $src_height, $thumb_width, $thumb_height, $align);
            $dest_width = $thumb_width;
            $dest_height = $thumb_height;
            break;
        case 'fit':
        default:
            list($dest_width, $dest_height) = scale_to_constraint($src_width, $src_height, $thumb_width, $thumb_height);
            $src_x = $src_y = 0;
    }
    // pre_dump( $src_x, $src_y, $dest_width, $dest_height, $src_width, $src_height );
    // Create a transparent image:
    $dest_imh = imagecreatetruecolor($dest_width, $dest_height);
    imagealphablending($dest_imh, true);
    imagefill($dest_imh, 0, 0, imagecolortransparent($dest_imh, imagecolorallocatealpha($dest_imh, 0, 0, 0, 127)));
    imagesavealpha($dest_imh, true);
    if (!imagecopyresampled($dest_imh, $src_imh, 0, 0, $src_x, $src_y, $dest_width, $dest_height, $src_width, $src_height)) {
        return array('!GD-library internal error (resample)', $dest_imh);
    }
    if ($thumb_percent_blur > 0) {
        // Apply blur effect
        $dest_imh = pixelblur($dest_imh, $dest_width, $dest_height, $thumb_percent_blur);
    }
    // TODO: imageinterlace();
    return array(NULL, $dest_imh);
}