PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 11, 2022

[FIXED] How to generate image file using this PHP function?

 October 11, 2022     gd, php     No comments   

Issue

I've been looking for a GD based solution for adding a perspective transformation to an image and eventually found something that seems promising: http://www.jqueryit.com/2010/03/set-perspective-of-image-using-php-gd.html

However, I'm not sure how to actually generate a new image using this function. My approach was this:

function perspective($i,$gradient=0.85,$rightdown=true,$background=0xFFFFFF) {
    $mult=5;
    $w=imagesx($i);
    $h=imagesy($i);
    $image=imagecreatetruecolor($w*$mult,$h*$mult);
    imagecopyresized($image,$i,0,0,0,0,$w*$mult,$h*$mult,$w,$h);
    imagedestroy($i);
    $w*=$mult;
    $h*=$mult;
    $im=imagecreatetruecolor($w,$h);
    $background=imagecolorallocate($im,($background>>16)&0xFF,($background>>8)&0xFF,$background&0xFF);
    imagefill($im,0,0,$background);
    imageantialias($im,true);
    $nh=$h-($h*$gradient);
    for ($x=0; $x<$w; $x++) {
        $ni=(($rightdown) ? $x : $w-$x);
        $p=intval($h-(($ni/$w)*$nh));
        if (($p%2)<>0)
            $p-=1;
        $nx=intval(($p-$h)/2);
        imagecopyresampled($im,$image,$x,0,$x,$nx,1,$p,1,$h-1);
        imageline($im,$x,0,$x,-$nx-1,$background);
        imageline($im,$x,$h-1,$x,$h+$nx,$background);
    }
    imagedestroy($image);
    imagefilter($im,IMG_FILTER_SMOOTH,10);
    $i=imagecreatetruecolor($w/$mult,$h/$mult);
    imageantialias($i,true);
    imagecopyresampled($i,$im,0,0,0,0,$w,$h,$w*$mult,$h*$mult);
    imagedestroy($im);
    return $i;
}

$image = perspective("my_image.jpg");

imagejpeg($image , "my_image_converted.jpg");

And unfortunately, it didn't produce an output. What is it that I'm doing wrong?


Solution

Because you can't pass just a filename when the function requires an image resource. Try:

$image = perspective(imagecreatefromjpeg("my_image.jpg"));

Read through the function you took from your link and look specifically where imagecopyresized() is called.



Answered By - WWW
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing