首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在PHP中调整图像大小

在PHP中调整图像大小
EN

Stack Overflow用户
提问于 2013-04-13 15:02:00
回答 3查看 188关注 0票数 0

我是一个非常新的在php.Basically中调整图像大小,我想使缩略图图像使用上传的图像。我已经使用了下面的代码,但它不工作,有人能帮助我吗?先谢谢你...

代码语言:javascript
运行
复制
$source_image = $path.$row->photosfolder;
$size = getimagesize($source_image);
$w = $size[0];
$h = $size[1];
$simg = imagecreatefromjpeg($source_image);
$dimg = imagecreatetruecolor(150, 225);
$wm = $w / 150;
$hm = $h / 225;
$h_height = 225 / 2;
$w_height = 150 / 2;

if ($w > $h) {
    $temp = imagecopyresampled($dimg, $simg, 0, 0, 0, 0, 150, 225, $w, $h);
}
elseif (($w < $h) || ($w == $h)) {
    $temp = imagecopyresampled($dimg, $simg, 0, 0, 0, 0, 150, 225, $w, $h);
}
else {
    $temp = imagecopyresampled($dimg, $simg, 0, 0, 0, 0, 150, 225, $w, $h);
}

$thumb_image = imagejpeg($dimg, $simg, 100);
EN

回答 3

Stack Overflow用户

发布于 2013-04-13 15:06:25

check timthumb.php很容易使用,你可以找到完整的教育代码

票数 0
EN

Stack Overflow用户

发布于 2013-04-13 15:20:56

如果你想调整图像的大小,你应该在客户端进行,因为PHP图像操作占用了大量的内存和CPU时间,而且不需要在服务器端完成(不需要访问db,不需要访问session等)。

如果您仍然希望在PHP上执行此操作,则可以使用这些函数来获得正确的大小:

代码语言:javascript
运行
复制
list($realW, $realH) = getimagesize($source_image);

$realR = $realW / $realH;
$thumbR = $thumbW / $thumbH;

// If you want your resize image to fit inside the max thumb size :

if($realR > $thumbR) // Real image if flatter than thumb
{
    $newW = $thumbW;
    $newH = $newW / $realR;
}
else
{
    $newH = $thumbH;
    $newW = $newH * $realR;
}

// Or if you want your resize image to be as small as possible but
// not smaller than your thumb. This can be helpful in some cases.

if($realR < $thumbR)
{
    // Same code
}

然后像你做的那样使用复制重采样(如果你不能让它工作,请阅读php手册,下面有函数概要的例子)。

如果要使用javascript调整图像大小,可以使用<canvas>

代码语言:javascript
运行
复制
var canvas = document.createElement('canvas');
var image = document.getElementById('image');
var context = canvas.getContext('2d');
context.save();
context.drawImage(image, /* Here you put the right values using the algorithms above */);
context.restore();
var thumb = document.createElement('image');
thumb.src = canvas.toDataUrl();

或者类似的东西。根据您的具体情况,您可能会更改一些内容。

票数 0
EN

Stack Overflow用户

发布于 2013-09-18 13:34:21

请尝试使用以下代码,它将调整图像大小并创建新的缩略图。新尺寸定义为100 X 100。此示例还将保持图像的纵横比。注意:

1.如果要设置完整路径,镜像路径将为目录路径。2.在我们考虑的示例中,对于jpg文件,您可以使用for PGN & GIF文件,其中imagecreatefrompng,imagecreatefromgif。3.这将创建PNG文件。

代码语言:javascript
运行
复制
$_imagePath = 'somefile.jpg';
$im = imagecreatefromjpeg($_imagePath);

imagealphablending($im, true);

$_orgWidth = imagesx($im);
$_orgHeight =  imagesy($im);

$_newWidth = 100;
$_newHeight = 100;

$_finalHeight = $_orgHeight * ( $_newWidth/ $_orgWidth);
    if($_finalHeight > $_newHeight){
        $_newWidth = $_orgWidth * ($_newHeight / $_orgHeight);
    }else{
        $_newHeight = $_finalHeight ;
    }


$_thumb = imagecreatetruecolor($_newWidth, $_newHeight);
imagealphablending($_thumb, true);
imagesavealpha($_thumb, true);

imagecopyresampled($_thumb, $im, 0, 0, 0, 0, $_newWidth, $_newHeight, $_orgWidth , $_orgHeight  );

imagepng($_thumb, 'newname.png');
imagedestroy($_thumb);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15985200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档