PHP实现批量生成App各种尺寸Logo_php技巧_脚本之家

php 切取图片代码

以上就是本文所述的全部内容了,希望大家能够喜欢。

注意提交的URL
method=”post”>

function resizeThumbnailImage($thumb_image_name, $image, $width,
$height, $start_width, $start_height, $scale){list($imagewidth,
$imageheight, $imageType) = getimagesize($image);$imageType =
image_type_to_mime_type($imageType);$newImageWidth = ceil($width *
$scale);$newImageHeight = ceil($height * $scale);$newImage =
imagecreatetruecolor($newImageWidth,$newImageHeight);switch($imageType)
{case image/gif:$source=imagecreatefromgif($image); break; case
image/pjpeg:case image/jpeg:case
image/jpg:$source=imagecreatefromjpeg($image); break; case
image/png:case image/x-png:$source=imagecreatefrompng($image); break;
}imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);switch($imageType)
{case image/gif: imagegif($newImage,$thumb_image_name); break; case
image/pjpeg:case image/jpeg:case image/jpg:
imagejpeg($newImage,$thumb_image_name,90); break;case image/png:case
image/x-png:imagepng($newImage,$thumb_image_name); break;
}chmod($thumb_image_name, 0777);return $thumb_image_name;}

使用PHP
GD,使用良好,一键剪裁各种尺寸,打包下载。经常换icon的懂的,美工给你一个1024的logo,你得ps出各种尺寸,于是有了这个东西。

1,首先你必须生成二维码具体代码如下:
复制代码 代码如下:
class QRCode{
public $w;
public $h;
public $s;
function __construct($w1,$h1,$s1){
$this->w = $w1;
$this->h = $h1;
$this->s = $s1;
$this->outimgase();
}
function qrcode(){
$post_data = array();
$post_data[‘cht’] = ‘qr’;
$post_data[‘chs’] = $this->w.”x”.$this->h;
$post_data[‘chl’] = $this->s;
$post_data[‘choe’] = “UTF-8”;
$url = “”;
$data_Array = array();
foreach($post_data as $key => $value)
{
$data_Array[] = $key.’=’.$value;
}
$data = implode(“&”,$data_Array);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function outimgase(){
echo $this->qrcode();
}
}
header(“Content-type:image/png”);
$t = new QRCode(300,300,”tianxin”);

复制代码 代码如下:source; } } /** *
set the source image * * @param string|array $source */ public
function source { if { $this->source[“name”] = $source;
$this->source[“tmp_name”] = $source; $type = NULL; $ext =
strtolower(end); switch { case “jpg” : case “jpeg” : $type =
“image/jpeg”; break; case “gif” : $type = “image/gif”; break; case “png”
: $type = “image/png”; break; } $this->source[“type”] = $type; }
else { $this->source = $source; } $this->destination =
$this->source[“name”]; } /** * resize the image * * @param int
$width * @param int $height */ public function resize($width =
NULL,$height = NULL) { if(isset($this->source[“tmp_name”]) &&
file_exists($this->source[“tmp_name”])) {
list($source_width,$source_height) =
getimagesize($this->source[“tmp_name”]); if && { $width =
($source_width * $height) / $source_height; } if && { $height =
($source_height * $width) / $source_width; } if && { $width =
$source_width; $height = $source_height; }
switch($this->source[“type”]) { case “image/jpeg” : $created =
imagecreatefromjpeg($this->source[“tmp_name”]); break; case
“image/gif” : $created =
imagecreatefromgif($this->source[“tmp_name”]); break; case
“image/png” : $created =
imagecreatefrompng($this->source[“tmp_name”]); break; }
$this->image = imagecreatetruecolor;
imagecopyresampled($this->image,$created,0,0,0,0,$width,$height,$source_width,$source_height);
} } /** * add watermark on image * * @param string $mark * @param
int $opac * @param int $x_pos * @param int $y_pos */ public
function watermark($mark,$opac,$x_pos,$y_pos) { if && { $ext =
strtolower(end; switch { case “jpg” : case “jpeg” : $watermark =
imagecreatefromjpeg; break; case “gif” : $watermark =
imagecreatefromgif; break; case “png” : $watermark = imagecreatefrompng;
break; } list($watermark_width,$watermark_height) = getimagesize;
$source_width = imagesx; $source_height = imagesy; if $pos = “t”; else
$pos = “b”; if $pos .= “l”; else $pos .= “r”; $dest_x = 0; $dest_y =
0; switch { case “tr” : $dest_x = $source_width – $watermark_width;
break; case “bl” : $dest_y = $source_height – $watermark_height;
break; case “br” : $dest_x = $source_width – $watermark_width;
$dest_y = $source_height – $watermark_height; break; }
imagecopymerge($this->image,$watermark,$dest_x,$dest_y,0,0,$watermark_width,$watermark_height,$opac);
} } /** * crop the image * * @param int $x * @param int $y *
@param int $width * @param int $height */ public function crop {
if(isset($this->source[“tmp_name”]) &&
file_exists($this->source[“tmp_name”]) && && {
switch($this->source[“type”]) { case “image/jpeg” : $created =
imagecreatefromjpeg($this->source[“tmp_name”]); break; case
“image/gif” : $created =
imagecreatefromgif($this->source[“tmp_name”]); break; case
“image/png” : $created =
imagecreatefrompng($this->source[“tmp_name”]); break; }
$this->image = imagecreatetruecolor;
imagecopy($this->image,$created,0,0,$x,$y,$width,$height); } } /**
* create final image file * * @param string $destination * @param
int $quality */ public function create($destination,$quality = 100) {
if { $extension = substr; switch { case “gif” :
imagegif($this->image,$destination,$quality); break; case “png” :
$quality = ceil – 1; imagepng($this->image,$destination,$quality);
break; default : imagejpeg($this->image,$destination,$quality);
break; } } } /** * check if extension is valid * */ public function
validate_extension() { if(isset($this->source[“tmp_name”]) &&
file_exists($this->source[“tmp_name”])) { $exts =
array(“image/jpeg”, “image/pjpeg”, “image/png”, “image/x-png”); $ext =
$this->source[“type”]; $valid = 0; $this->ext = ‘.not_found’;
if ($ext == $exts[0] || $ext == $exts[1]) { $valid = 1;
$this->ext = ‘.jpg’; } // if { // $valid = 1; // $this->ext =
‘.gif’; // } if ($ext == $exts[2] || $ext == $exts[3]) { $valid = 1;
$this->ext = ‘.png’; } if { $this->error .= “extension”; } } else
{ $this->error .= “source”; } } /** * check if the size is correct
* * @param int $max */ public function validate_size {
if(isset($this->source[“tmp_name”]) &&
file_exists($this->source[“tmp_name”])) { $max = $max * 1024;
if($this->source[“size”] >= $max) { $this->error .= “size”; }
} else { $this->error .= “source”; } } /** * check if the
dimension is correct * * @param int $limit_width * @param int
$limit_height */ public function
validate_dimension($limit_width,$limit_height) {
if(isset($this->source[“tmp_name”]) &&
file_exists($this->source[“tmp_name”])) {
list($source_width,$source_height) =
getimagesize($this->source[“tmp_name”]); if(($source_width >
$limit_width) || ($source_height > $limit_height)) {
$this->error .= “dimension”; } } else { $this->error .= “source”;
} } /** * get the found errors * */ public function error;
if(stristr($this->error,”source”)) $error[] = “找不到上传文件”;
if(stristr($this->error,”dimension”)) $error[] =
“上传图片尺寸太大”; if(stristr($this->error,”extension”)) $error[]
= “不符合要求的格式”; if(stristr $error[] = “图片文件太大”; return
$error; } public function error_string() { $error = “”;
if(stristr($this->error,”source”)) $error .= “找不到上传文件 / “;
if(stristr($this->error,”dimension”)) $error .= “上传图片尺寸太大 /
“; if(stristr($this->error,”extension”)) $error .= “不符合要求的格式
/ “; if(stristr $error .= “图片文件太大 / “; if) { $error = substr; }
return $error; } public function ext() { return $this->ext; }}

// 生成图片
$imageFun = ‘image’ . ($type == ‘jpg’ ? ‘jpeg’ : $type);
$imageFun($thumbImg, $thumbname);
imagedestroy($thumbImg);
imagedestroy($srcImg);
return $thumbname;
}
return false;
}
function water($source, $thumb, $savename=””, $alpha=100){
//检查文件是否存在
if (!file_exists($source) || !file_exists($thumb))
return false;
//图片信息
$sInfo = getImageInfo($source);
$water =
thumb($thumb,”wy.jpg”,”jpg”,$sInfo[“width”]/4,$sInfo[“height”]/4);
$wInfo = getImageInfo($water);
//如果图片小于水印图片,不生成图片
if ($sInfo[“width”] < $wInfo[“width”] || $sInfo[‘height’] <
$wInfo[‘height’])
return false;
//建立图像
$sCreateFun = “imagecreatefrom” . $sInfo[‘type’];
$sImage = $sCreateFun($source);
$wCreateFun = “imagecreatefrom” . $wInfo[‘type’];
$wImage = $wCreateFun($water);
//设定图像的混色模式
imagealphablending($wImage, true);
//图像位置,默认为右下角右对齐
// $posY = $sInfo[“height”] – $wInfo[“height”];
// $posX = $sInfo[“width”] – $wInfo[“width”];
$posY = ($sInfo[“height”] – $wInfo[“height”])/2;
$posX = ($sInfo[“width”] – $wInfo[“width”])/2;
//生成混合图像
imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[‘width’],
$wInfo[‘height’], $alpha);
//输出图像
$ImageFun = ‘Image’ . $sInfo[‘type’];
//如果没有给出保存文件名,默认为原图像名
if (!$savename) {
$savename = $source;
@unlink($source);
}
//保存图像
$ImageFun($sImage, $savename);
imagedestroy($sImage);
}
water($source,$water);

中间可以自己定义图片的二维码生成器

二维码要生的内容:

希望能添加自己的图片地址:

<>

2,然后通过一个php文件将二维码和你的目的图片画在一起代码如下:

复制代码 代码如下:
$surl = $_POST[“url”];
function GrabImage($url,$filename=””) {
if($url==””):return false;endif;
if($filename==””) {
$ext=strrchr($url,”.”);
if($ext!=”.gif” && $ext!=”.jpg”):return false;endif;
$filename=date(“dMYHis”).$ext;
}
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2=@fopen($filename, “a”);
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}
$source =
GrabImage(“”);
$water =GrabImage($surl,”t.png”);
function getImageInfo($img){
$imageInfo = getimagesize($img);
if ($imageInfo !== false) {
$imageType =
strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
$imageSize = filesize($img);
$info = array(
“width” => $imageInfo[0],
“height” => $imageInfo[1],
“type” => $imageType,
“size” => $imageSize,
“mime” => $imageInfo[‘mime’]
);
return $info;
} else {
return false;
}
}
function thumb($image, $thumbname, $type=”, $maxWidth=200,
$maxHeight=50, $interlace=true) {
// 获取原图信息
$info = getImageInfo($image);
if ($info !== false) {
$srcWidth = $info[‘width’];
$srcHeight = $info[‘height’];
$type = empty($type) ? $info[‘type’] : $type;
$type = strtolower($type);
$interlace = $interlace ? 1 : 0;
unset($info);
$scale = min($maxWidth / $srcWidth, $maxHeight / $srcHeight); //
计算缩放比例
if ($scale >= 1) {
// 超过原图大小不再缩略
$width = $srcWidth;
$height = $srcHeight;
} else {
// 缩略图尺寸
$width = (int) ($srcWidth * $scale);
$height = (int) ($srcHeight * $scale);
}
// 载入原图
$createFun = ‘ImageCreateFrom’ . ($type == ‘jpg’ ? ‘jpeg’ : $type);
$srcImg = $createFun($image);
//创建缩略图
if ($type != ‘gif’ && function_exists(‘imagecreatetruecolor’))
$thumbImg = imagecreatetruecolor($width, $height);
else
$thumbImg = imagecreate($width, $height);
// 复制图片
if (function_exists(“ImageCopyResampled”))
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height,
$srcWidth, $srcHeight);
else
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height,
$srcWidth, $srcHeight);
if (‘gif’ == $type || ‘png’ == $type) {
//imagealphablending($thumbImg, false);//取消默认的混色模式
//imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
$background_color = imagecolorallocate($thumbImg, 0, 255, 0); //
指派一个绿色
imagecolortransparent($thumbImg, $background_color); //
设置为透明色,若注释掉该行则输出绿色的图
}
// 对jpeg图形设置隔行扫描
if (‘jpg’ == $type || ‘jpeg’ == $type)
imageinterlace($thumbImg, $interlace);

3,在来一个入口文件index.html 代码如下:
复制代码 代码如下:

中间可以自己定义图片的二维码生成器

在上面的代码中用3个函数 GrabImage()函数是将生成二维码的文件转化成图片
接下来的函数就是处理图片的缩放 将目的图片添加到二位上。

发表评论

电子邮件地址不会被公开。 必填项已用*标注