PHP图片裁剪

简介: PHP的GD库还是很强悍的。之前没玩过,自己根据搜的示例随便试了下 >;<<?phpfunction resize($newWidth, $targetFile, $originalFile, $xyz) { $info = getimagesize($originalFile); $mime = $info['mime']

PHP的GD库还是很强悍的。之前没玩过,自己根据搜的示例随便试了下 >;<

<?php

function resize($newWidth, $targetFile, $originalFile, $xyz) {

    $info = getimagesize($originalFile);
    $mime = $info['mime'];

    $randomTmp = rand(0, 10000);

    switch ($mime) {
            case 'image/jpeg':
                    $image_create_func = 'imagecreatefromjpeg';
                    $image_save_func = 'imagejpeg';
                    $new_image_ext = 'jpg';
                    break;

            case 'image/png':
                    $image_create_func = 'imagecreatefrompng';
                    $image_save_func = 'imagepng';
                    $new_image_ext = 'png';
                    break;

            case 'image/gif':
                    $image_create_func = 'imagecreatefromgif';
                    $image_save_func = 'imagegif';
                    $new_image_ext = 'gif';
                    break;

            default: 
                    throw new Exception('Unknown image type.');
    }

    $img = $image_create_func($originalFile);
    list($width, $height) = getimagesize($originalFile);

    $newHeight = ($height / $width) * $newWidth;
    $tmp = imagecreatetruecolor($newWidth, $newHeight);

    imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    $image_save_func($tmp, "$randomTmp.$new_image_ext");

    $img2 = $image_create_func("$randomTmp.$new_image_ext");

    $tmp2 = imagecreatetruecolor(500, 238);

    imagecopyresampled($tmp2, $img2, 0, 0, $xyz[0], $xyz[1], 500, (238 + $xyz[1]), $newWidth, (238 + $xyz[1]));

    if (file_exists("$randomTmp.$new_image_ext")) {
        unlink("$randomTmp.$new_image_ext");
    }

    if (file_exists($targetFile)) {
        unlink($targetFile);
    }
    $image_save_func($tmp2, "$targetFile.$new_image_ext");
}

$originalFile = __DIR__ . "/test.png";
$targetFile = __DIR__ . "/xxx";

resize(500, $targetFile, $originalFile, array(0, 150)); 

test.png:
test.png

xxx.png:
这里写图片描述

目录
相关文章
|
3月前
|
PHP
使用PHP实现随机调用图片
使用PHP实现随机调用图片
59 0
使用PHP实现随机调用图片
|
4月前
|
小程序 PHP 数据安全/隐私保护
php图片加水印函数
这里分享下php给图片加水印的几个自定义函数 给图片加水印首先需要开启GD库。 用到的php函数是imagecopymerge () 和 imagecopy () imagecopymerge 函数可以支持两个图像叠加时,设置叠加的透明度
45 0
|
6月前
|
PHP
【PHP】读取本地文件夹中所有图片并显示
PHP图片收集系统收集作业后,为了方便老师在线查阅作业,特意写了个读取图片然后显示出来的php 比较粗糙,可以再多美化美化
59 0
|
1月前
|
存储 PHP Apache
使用CFimagehost源码搭建无需数据库支持的PHP免费图片托管私人图床
使用CFimagehost源码搭建无需数据库支持的PHP免费图片托管私人图床
|
9月前
|
PHP
PHP实现自制随机图片API- 调用文件夹和引用网络图片
PHP实现随机图片API- 调用文件夹和引用网络图片
108 0
|
6月前
|
小程序 PHP
[微擎]多系统共用accesstoken修复wifi小程序文本敏感词汇检测+图片检测原生php(可用)
[微擎]多系统共用accesstoken修复wifi小程序文本敏感词汇检测+图片检测原生php(可用)
|
9月前
|
JSON 前端开发 API
layui框架实战案例(8):web图片裁切插件croppers.js组件实现上传图片的自定义截取(含php后端)
layui框架实战案例(8):web图片裁切插件croppers.js组件实现上传图片的自定义截取(含php后端)
378 0
|
9月前
|
PHP
PHP实现图片登录验证码的解决方案
PHP实现图片登录验证码的解决方案
75 0
|
9月前
|
API PHP
php访问API将图片保存到本地
php访问API将图片保存到本地
47 0