草庐IT

PHP GD图像透视

coder 2024-04-23 原文

您好,是否可以转换图像透视图。所以它的新形状是等腰梯形? 我看到了一个使用 Imagick 的解决方案,但这可能涉及重写我的整个图像处理脚本......(更不用说学习了,我对它过敏)

最佳答案

我改进了 James 发布的功能。

添加:

  • 垂直/水平透视支持
  • 透明度
  • 现在看起来更像是 3d 中的旋转对象(对象越近越大,距离越远越小)

这是我的功能:

define("TOP",0);
define("BOTTOM",1);
define("LEFT",2);
define("RIGHT",3);

function perspective($i,$gradient=0.85,$rightdown=TOP,$background=0xFFFFFF, $alpha=0) {
        $w=imagesx($i);
        $h=imagesy($i);
        $col=imagecolorallocatealpha($i,($background>>16)&0xFF,($background>>8)&0xFF,$background&0xFF,$alpha);

        $mult=5;
        $li=imagecreatetruecolor($w*$mult,$h*$mult);
        imagealphablending($li,false);
        imagefilledrectangle($li,0,0,$w*$mult,$h*$mult,$col);
        imagesavealpha($li,true);

        imagecopyresized($li,$i,0,0,0,0,$w*$mult,$h*$mult,$w,$h);
        imagedestroy($i);
        $w*=$mult;
        $h*=$mult;


        $image=imagecreatetruecolor($w,$h);
        imagealphablending($image,false);
        imagefilledrectangle($image,0,0,$w,$h,$col);
        imagealphablending($image,true);

        imageantialias($image,true);
        $test=$h*$gradient;

        $rdmod=$rightdown%2;
        $min=1;
        if($rightdown<2){
            for($y=0;$y<$h;$y++){
                $ny=$rdmod? $y : $h-$y;
                $off=round((1-$gradient)*$w*($ny/$h));
                $t=((1-pow(1-pow(($ny/$h),2),0.5))*(1-$gradient)+($ny/$h)*$gradient);
                $nt=$rdmod? $t : 1-$t;
                if(abs(0.5-$nt)<$min){
                    $min=abs(0.5-$nt);
                    $naty=$off;
                }
                imagecopyresampled($image,$li,
                                    round($off/2),$y,
                                    0,abs($nt*$h),
                                    $w-$off,1,
                                    $w,1);
            }
        } else {
            for($x=0;$x<$w;$x++){
                $nx=$rdmod? $x : $w-$x;
                $off=round((1-$gradient)*$h*($nx/$w));
                $t=((1-pow(1-pow(($nx/$w),2),0.5))*(1-$gradient)+($nx/$w)*$gradient);
                $nt=$rdmod? $t : 1-$t;
                if(abs(0.5-$nt)<$min){
                    $min=abs(0.5-$nt);
                    $natx=$off;
                }
                imagecopyresampled($image,$li,
                                    $x,round($off/2),
                                    abs($nt*$w),0,
                                    1,$h-$off,
                                    1,$h);
            }
        }
        imagedestroy($li);

        imageantialias($image,false);
        imagealphablending($image,false);
        imagesavealpha($image,true);

        $i=imagecreatetruecolor(($w+$naty)/$mult,($h+$natx)/$mult);
        imagealphablending($i,false);
        imagefilledrectangle($i,0,0,($w+$naty)/$mult,($h+$natx)/$mult,$col);
        imagealphablending($i,true);
        imageantialias($i,true);
        imagecopyresampled($i,$image,0,0,0,0,($w+$naty)/$mult,($h+$natx)/$mult,$w,$h);
        imagedestroy($image);
        imagealphablending($i,false);
        imageantialias($i,false);
        imagesavealpha($i,true);
        return $i;
    }

例子:

Original image: http://imgur.com/iX5Aski

使用函数:

$img=perspective($img,0.5,TOP,0xFFFFFF,127);

结果:

$img=perspective($img,0.2,RIGHT,0xFFFFFF,127);

结果:

关于PHP GD图像透视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1503646/

有关PHP GD图像透视的更多相关文章

  1. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

  2. ruby-on-rails - 在 Ruby (on Rails) 中使用 imgur API 获取图像 - 2

    我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path

  3. python ffmpeg 使用 pyav 转换 一组图像 到 视频 - 2

    2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p

  4. ruby - 是否有将图像文件转换为 ASCII 艺术的命令行程序或库? - 2

    有这样的事吗?我想在Ruby程序中使用它。 最佳答案 试试这个http://csl.sublevel3.org/jp2a/此外,Imagemagick可能还有一些东西 关于ruby-是否有将图像文件转换为ASCII艺术的命令行程序或库?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6510445/

  5. ruby-on-rails - 使用 Dragonfly 从 URL 分配图像 - 2

    我正在使用Dragonfly在Rails3.1应用程序上处理图像。我正在努力通过url将图像分配给模型。我有一个很好的表格:{:multipart=>true}do|f|%>RemovePicture?Dragonfly的文档指出:Dragonfly提供了一个直接从url分配的访问器:@album.cover_image_url='http://some.url/file.jpg'但是当我在控制台中尝试时:=>#ruby-1.9.2-p290>picture.image_url="http://i.imgur.com/QQiMz.jpg"=>"http://i.imgur.com/QQ

  6. Ruby-vips 图像处理库。有什么好的使用示例吗? - 2

    我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby​​代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby​​-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby​​-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby​​-vips的github页面上的链接,我们将不胜感激!如果有ruby​​-

  7. ruby-on-rails - 如何播种图像的路径? - 2

    Organization和Image具有一对一的关系。Image有一个名为filename的列,它存储文件的路径。我在Assets管道中包含这样一个文件:app/assets/other/image.jpg。播种时如何包含此文件的路径?我已经在我的种子文件中尝试过:@organization=...@organization.image.create!(filename:File.open('app/assets/other/image.jpg'))#Ialsotried:#@organization.image.create!(filename:'app/assets/other/i

  8. ruby-on-rails - 安全地显示使用回形针 gem 上传的图像 - 2

    默认情况下:回形针gem将所有附件存储在公共(public)目录中。出于安全原因,我不想将附件存储在公共(public)目录中,所以我将它们保存在应用程序根目录的uploads目录中:classPost我没有指定url选项,因为我不希望每个图像附件都有一个url。如果指定了url:那么拥有该url的任何人都可以访问该图像。这是不安全的。在user#show页面中:我想实际显示图像。如果我使用所有回形针默认设置,那么我可以这样做,因为图像将在公共(public)目录中并且图像将具有一个url:Someimage:看来,如果我将图像附件保存在公共(public)目录之外并且不指定url(同

  9. ruby - Paperclip:以编程方式分配图像并设置其名称 - 2

    使用Paperclip,我想从这样的URL抓取图像:require'open-uri'user.photo=open(url)问题是我最后得到一个像“open-uri20110915-4852-1o7k5uw”这样的文件名。有什么方法可以更改user.photo上的文件名?作为一个额外的变化,Paperclip将我的文件存储在S3上,所以如果我可以在初始分配中设置我想要的文件名就更好了,这样图像就会上传到正确的S3key。像这样:user.photo=open(url),:filename=>URI.parse(url).path 最佳答案

  10. ruby-on-rails - 如何在回形针 ruby​​ on rails 中设置默认图像 - 2

    最近我安装了Paperclipgem,我正在努力让默认图像在我的系统上工作,我将图像文件放在assets/images/pic.png中。这是我的模型User中的代码:has_attached_file:pic,:styles=>{:medium=>"300x300>",:thumb=>"100x100>"},:default_url=>'missing_:avatar.png'#:default_url=>'assets/images/avatar.png'has_attached_file:attach这是我的AddPicPaperClip迁移中的代码:defself.upadd_

随机推荐