繁体   English   中英

PHP imagecopyresampled()不带高度

[英]PHP imagecopyresampled() without height

我这里有这段代码

imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);

Baiscally我想做的是上传图像并调整宽度,并根据宽度调整高度。

我也尝试过

imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width);

没有高度,并得到此错误

Warning: Wrong parameter count for imagecopyresampled() in /home/content/44/8713044/html/admin/Categories.php on line 63

这是$width$height变量来自的当前代码。

if($width> $height) {
            $x = ceil(($width - $height) / 2 );
            $width = $height;
        } elseif($height> $width) {
            $y = ceil(($height - $width) / 2);
            $height = $width;
        }

任何帮助,将不胜感激,谢谢在高级,J

这是全部功能。

function create_thumbnail($source,$destination, $thumb_width) {
        $percent = 0.5;
        $size = getimagesize($source);
        $width = $size[0];
        $height = $size[1];
        $x = 0;
        $y = 0;
        if($width> $height) {
            $x = ceil(($width - $height) / 2 );
            $width = $height;
        } elseif($height> $width) {
            $y = ceil(($height - $width) / 2);
            $height = $width;
        }
        $new_image = imagecreatetruecolor($thumb_width,$thumb_width)or die('Cannot Initialize new GD image stream');
        $extension = get_image_extension($source);
         if($extension=='jpg' || $extension=='jpeg') 
            $image = imagecreatefromjpeg($source); 
        if($extension=='gif') 
            $image = imagecreatefromgif($source); 
        if($extension=='png') 
            $image = imagecreatefrompng($source);   

        imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
        if($extension=='jpg' || $extension=='jpeg') 
           imagejpeg($new_image,$destination); 
        if($extension=='gif') 
            imagegif($new_image,$destination); 
        if($extension=='png') 
            imagepng($new_image,$destination); 
    }

$thumb_width为600,这将返回我的图片600 * 600

当我测试它的时候,这个效果很好....除了您要使用固定大小

$filename = "a.jpg" ;
$percent = 0.5;
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, null, 100);

演示版

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM