php内容类型图像大小 [英] php content-type image size

查看:71
本文介绍了php内容类型图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码来显示用户的头像。

I have this code to display users avatars..

<?php
include("../core/config.php");

if(isset($_GET['uid'])){
if(is_numeric($_GET['uid'])){
    $uid = $_GET['uid'];
}
else{
    exit();
}

$sql="SELECT avatar FROM users_avatar WHERE user_id = '$uid'";  
$row= getRow($sql);
if(!$row){
    $url = "../usravatars/_default/usravatar_default_m.png";
}
else{
$avatar = $row['avatar'];
$url = "../usravatars/$uid/$avatar";
}   
header("Content-Type: image/jpg");
readfile($url);
}
?>

是否可以为显示的图像设置自定义尺寸?

Is possible to set a custom size for the image being displayed?

推荐答案

以下是一个示例函数,可以即时调整图像大小。您可以使用自己的特定宽度/高度或从get变量中获取。

Following is a sample function to resize the image on the fly. You can use your own specific width/height or fetch from the get variables.

function CroppedThumbnail($imgSrc,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE - Returns an image resource.
//getting the image dimensions  
list($width_orig, $height_orig) = getimagesize($imgSrc);   
$myImage = imagecreatefromjpeg($imgSrc);
$ratio_orig = $width_orig/$height_orig;

if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
   $new_height = $thumbnail_width/$ratio_orig;
   $new_width = $thumbnail_width;
} else {
   $new_width = $thumbnail_height*$ratio_orig;
   $new_height = $thumbnail_height;
}

$x_mid = $new_width/2;  //horizontal middle
$y_mid = $new_height/2; //vertical middle

$process = imagecreatetruecolor(round($new_width), round($new_height)); 

imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
$thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height); 
imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);

imagedestroy($process);
imagedestroy($myImage);
return $thumb;
}

//Create the thumbnail
$newThumb = CroppedThumbnail("DSC01088.jpg",$_GET['width'],$_GET['height']);

// And display the image...
header('Content-type: image/jpeg');
imagejpeg($newThumb);

这篇关于php内容类型图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆