首页 > php教程 > php手册 > 正文

教你用PHP写生成缩略图代码

php中文网
发布: 2016-06-06 19:37:17
原创
991人浏览过

源文件,目标文件,目标宽,目标高,是否允许剪裁。 如果目标写入null直接二进制输出。不生成文件。 无 function img2thumb($src_img, $dst_img, $width = 64, $height = 64, $nocut = 0) {/*if(!is_file($src_img)) return false;*/if(!($width*$height)) re

源文件,目标文件,目标宽,目标高,是否允许剪裁。
如果目标写入 null 直接二进制输出。不生成文件。
function img2thumb($src_img, $dst_img, $width = 64, $height = 64, $nocut = 0) {
	/*
	if(!is_file($src_img)) return false;
	*/
	if(!($width*$height)) return false;

	$ext = strtolower(substr(strrchr($src_img, '.'),1));
	if(!$ext) return false;
	$otfunc = 'image' . ($ext == 'jpg' ? 'jpeg' : $ext);
	if(!function_exists($otfunc)) return false;

	$srcinfo = getimagesize($src_img);
	if(!$srcinfo) return false;

	$src_w = $srcinfo[0];
	$src_h = $srcinfo[1];
	$type  = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
	$openfunc = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);
	if(!function_exists($openfunc)) return false;

	$createfunc = 'imagecreate';
	$buildfunc = 'imagecopyresized';

	$x=$y=0;
	$src_t = $src_w / $src_h;
	if($src_t > $width / $height) {
		$dst_w = $width;
		$dst_h = $width / $src_t;
		$nocut && $y = ($height - $dst_h)/2;
	}
	else {
		$dst_w = $src_t * $height;
		$dst_h = $height;
		$nocut && $x = ($width - $dst_w)/2;
	}

	$src = $openfunc($src_img);
	$dst = $createfunc($nocut ? $width :$dst_w, $nocut ? $height :$dst_h);
	$white = imagecolorallocate($dst, 255, 255, 255);
	$buildfunc($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
    $otfunc($dst, $dst_img);
    imagedestroy($dst);
    imagedestroy($src);
    return true;
}
登录后复制
<?php
// 更新,增加参数 $watermark, 如果这个文件是可用的,将作为水印加在图片上,$wmpct透明度.

function img2thumb($src_img, $dst_img, $width=64, $height=64, $nocut=0, $watermark=null, $wmpct=50) {
	/*
	if(!is_file($src_img)) return false;
	*/
	if(!($width*$height)) return false;

	$ext = strtolower(substr(strrchr($src_img, '.'),1));
	if(!$ext) return false;
	$otfunc = 'image' . ($ext == 'jpg' ? 'jpeg' : $ext);
	if(!function_exists($otfunc)) return false;


	$srcinfo = getimagesize($src_img);
	if(!$srcinfo) return false;

	$src_w = $srcinfo[0];
	$src_h = $srcinfo[1];
	$type  = strtolower(substr(image_type_to_extension($srcinfo[2]), 1));
	$openfunc = 'imagecreatefrom' . ($type == 'jpg' ? 'jpeg' : $type);
	if(!function_exists($openfunc)) return false;

	$wmfunc = '';
	if($watermark) {
		$wminfo=getimagesize($watermark);
		if($wminfo) {
			$wm_w = $wminfo[0];
			$wm_h = $wminfo[1];
			$wmtype  = strtolower(substr(image_type_to_extension($wminfo[2]), 1));
			$wmfunc = 'imagecreatefrom' . ($wmtype == 'jpg' ? 'jpeg' : $wmtype);
			if(function_exists($wmfunc)) {
			}
		}
	}

	$createfunc = 'imagecreate';
	$buildfunc = 'imagecopyresized';

	$x=$y=0;
	$src_t = $src_w / $src_h;
	if($src_t > $width / $height) {
		$dst_w = $width;
		$dst_h = $width / $src_t;
		$nocut && $y = ($height - $dst_h)/2;
	}
	else {
		$dst_w = $src_t * $height;
		$dst_h = $height;
		$nocut && $x = ($width - $dst_w)/2;
	}

	if(!$dst_img) {
		header ( "Content-type: " . image_type_to_mime_type( IMAGETYPE_JPEG ));
	}

	$src = $openfunc($src_img);
	$dst = $createfunc($nocut ? $width :$dst_w, $nocut ? $height :$dst_h);
	$white = imagecolorallocate($dst, 255, 255, 255);
	$buildfunc($dst, $src, $x, $y, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
	imagedestroy($src);

	// 水印
	if($wmfunc && function_exists($wmfunc)) {
		$wm = $wmfunc($watermark);
		imagecopymergegray($dst, $wm, $x+$dst_w-$wm_w, $y+$dst_h-$wm_h, 0, 0, $wm_w, $wm_h, $wmpct);
		imagedestroy($wm);
	}

	$otfunc($dst, $dst_img);
	imagedestroy($dst);
	return true;
}
登录后复制
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号