* index.php
<?php
/**
* Created by PhpStorm.
* User: mingzhanghui
* Date: 9/24/2019
* Time: 12:47
*/
include "autoload.php";
// 头像图片
$dstPath = dirname(__FILE__)."/image/avatar.jpg";
// 国旗图片
$srcPath = dirname(__FILE__)."/image/flag.png";
/** @var $dataDst string image content */
$dataDst = file_get_contents($dstPath);
$dataSrc = file_get_contents($srcPath);
$dst = imagecreatefromstring($dataDst);
list($dstW, $dstH) = getimagesize($dstPath);
list($srcW, $srcH) = getimagesize($srcPath);
// 宽度是小图的3倍
$thumbWidth = 3 * $srcW;
$thumbHeight = $thumbWidth * ($dstH/$dstW);
$data = \lib\Image::resize($dataDst, $thumbWidth, $thumbHeight, 0);
if (!$data) {
echo "resize failed\n";
exit(1);
}
$dst = imagecreatefromstring($data);
$info = getimagesize($dstPath);
// 'mime' => 'image/jpeg'
// 'mime' => 'image/png'
$a = explode('/', $info['mime']);
$type = array_pop($a);
unset($a);
$dstPath = "tmp.".$type;
call_user_func("image".$type, $dst, $dstPath);
// == 缩略图生成完了 ==
$dstW = $thumbWidth;
$dstH = $thumbHeight;
list($dstW, $dstH) = getimagesize($dstPath);
$src = imagecreatefromstring($dataSrc);
imagecopymerge($dst, $src,
$dstW - $srcW, $dstH - $srcH,
0, 0, $srcW, $srcH, 100);
/*
header("Content-Type: ".$info['mime']);
// imagepng();
call_user_func("image".$type, $dst);
*/
call_user_func("image".$type, $dst, "out.".$type);
imagedestroy($src);
imagedestroy($dst);
* autoload.php
<?php
/**
* Created by PhpStorm.
* User: mingzhanghui
* Date: 9/24/2019
* Time: 13:16
*/
$prefixList = ['lib'];
$pwd = dirname(__FILE__);
foreach ($prefixList as $prefix) {
spl_autoload_register(function($class) use ($prefix, $pwd) {
$base_dir = $pwd . DIRECTORY_SEPARATOR. str_replace('\\', '/', $prefix);
// echo $base_dir.PHP_EOL;
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// echo $file.PHP_EOL;
if (file_exists($file)) {
require $file;
}
});
}
* ./lib/Image.php
<?php
/**
* Created by PhpStorm.
* User: mingzhanghui
* Date: 9/24/2019
* Time: 13:15
*/
namespace lib;
class Image
{
/**
* @param $imagedata string 图像数据
* @param $width int 缩放宽度
* @param $height int 缩放高度
* @param int $per 缩放比例,为0不缩放,>0忽略参数2、3的宽高
* @return bool|string
*/
public static function resize($imagedata, $width, $height, $per = 0) {
// 1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
// 获取图像信息
list($bigWidth, $bigHight, $bigType) = getimagesizefromstring($imagedata);
// 缩放比例
if ($per > 0) {
$width = $bigWidth * $per;
$height = $bigHight * $per;
}
// 创建缩略图画板
$block = imagecreatetruecolor($width, $height);
// 启用混色模式
imagealphablending($block, false);
// 保存PNG alpha通道信息
imagesavealpha($block, true);
// 创建原图画板
$bigImg = imagecreatefromstring($imagedata);
// 缩放
imagecopyresampled($block, $bigImg, 0, 0, 0, 0,
$width, $height, $bigWidth, $bigHight);
// 生成临时文件名
$tmpFilename = tempnam(sys_get_temp_dir(), 'image_');
// 保存
switch ($bigType) {
case 1: imagegif($block, $tmpFilename);
break;
case 2: imagejpeg($block, $tmpFilename);
break;
case 3: imagepng($block, $tmpFilename);
break;
}
// 销毁
imagedestroy($block);
$image = file_get_contents($tmpFilename);
unlink($tmpFilename);
return $image;
}
}
把国旗图片覆盖到目的图片右下角