使用imagerotate()旋转图像后如何获得新的宽度和高度?

时间:2015-02-27 09:54:36

标签: php

如何实际获得旋转图像后设置的新宽度和高度?

$ps['product_angle'] = 77; //Could be any angle
$filename = 'test.png'     //filename to the original product

list($source_width, $source_height) = getimagesize($filename);
$source_image = imagecreatefromjpeg($filename);         
$angle = $ps['product_angle'];
if (intval($angle) <> 0) {
    $source_image = imagerotate($source_image, 360-$angle, imageColorAllocateAlpha($source_image, 255, 255, 255, 127));
}

$ps['source_image'] = $source_image;

我想要这个,因为我想根据上面创建的图像调整图像大小。 ($ps['source_image']

//If I do an image 
list($source_width, $source_height) =     getimagesize($filename);

$dest_width = (int)$ps['product_width'];
$dest_height = (int)$ps['product_height'];

//Resize source-image to new width and height
//But this width and height are incorrect because they are 
//set before image is rotated and often the image is just "cut off" 
//
imagecopyresized($dest_image, $ps['source_image'], 0, 0, 0, 0, $dest_width, $dest_height, $source_width, $source_height);    

2 个答案:

答案 0 :(得分:6)

使用函数imagesx()imagesy()来获取使用GD在内存中加载或创建的图像的宽度和高度。

$filepath = '/tmp/1.jpg';
$size = getimagesize($filepath);
echo('Image dimensions returned by getimagesize()   : '.$size[0].'x'.$size[1]." pixels\n");

$img  = imagecreatefromjpeg($filepath);
$width  = imagesx($img);
$height = imagesy($img);
echo('Dimensions returned by imagesx() and imagesy(): '.$width.'x'.$height." pixels.\n");

$angle = 60;
$dst = imagerotate($src, $angle, imageColorAllocateAlpha($src, 255, 255, 255, 127));
$width  = imagesx($dst);
$height = imagesy($dst);
echo('Dimensions of the rotated image: '.$width.'x'.$height." pixels.\n");

答案 1 :(得分:0)

您可以使用:http://php.net/manual/en/function.getimagesize.php,例如:

<?php
$size = getimagesize($filename);
$fp = fopen($filename, "rb");
if ($size && $fp) {
    header("Content-type: {$size['mime']}");
    fpassthru($fp);
    exit;
} else {
    // error
}
?>