php 生成 二进制,php 如何把二进制数据流生成缩略图

/*

二进制数据流生成缩略图

@$data 二进制图像数据流

@$mimetype 图像类型

@$thumbnailHeight 缩略图高度

@$thumbnailWidth 缩略图宽度

return 缩略图的二进制数据流

*/

function _render_thumbnail($data, $mimetype,$thumbnailHeight=60,$thumbnailWidth=100)

{

//echo $data;

// file is not an image

if ($mimetype !== 'image/jpeg' &&

$mimetype !== 'image/pjpeg' &&

$mimetype !== 'image/gif'  &&

$mimetype !== 'image/png')

{

throw new Exception('Unsupported file type.');

}

$ret = '';

$imageType = substr($mimetype, 6);

$image = imagecreatefromstring($data);

$width  = imagesx($image);

$height = imagesy($image);

// decrease image size if height exceeds thumbnailHeight

if ($height > $thumbnailHeight)

{

$factor = $thumbnailHeight / $height;

$targetWidth = round($factor * $width);

$imageNew = imagecreatetruecolor($targetWidth, $thumbnailHeight);

imagecopyresampled($imageNew, $image, 0, 0, 0, 0, $targetWidth, $thumbnailHeight, $width, $height);

}

// otherwise, check for width

else if ($width > $thumbnailWidth)

{

$factor = $thumbnailWidth / $width;

$targetHeight = round($factor * $width);

$imageNew = imagecreatetruecolor($thumbnailWidth, $targetHeight);

imagecopyresampled($imageNew, $image, 0, 0, 0, 0, $thumbnailWidth, $targetHeight, $width, $height);

}

// if the image is small enough, we take it as-is

else

{

return $data;

}

// store new image data in a variable

ob_start();

switch ($imageType)

{

case 'jpeg' : imagejpeg($imageNew, null, 100);

break;

case 'pjpeg' : imagejpeg($imageNew, null, 100);

break;

case 'gif'  : imagegif($imageNew);

break;

case 'png'  : imagepng($imageNew, null, 0);

break;

}

imagedestroy($imageNew);

$ret = ob_get_clean();

return $ret;

}

?>

主要是 imagecreatefromstring 函数的使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值