webdevRefinery Forum: [Solved]Trouble cropping transparent image - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: -----

User is offline Sole_Wolf 

  • Group: Members
  • Posts: 303
  • Joined: 24-March 10
  • Expertise:HTML,PHP,Java,SQL

Posted 28 April 2012 - 09:42 PM (#1)

[Solved]Trouble cropping transparent image


I found this code online, and it appears to work perfectly. But later on, I tried to crop a .png with a transparent background and black in it and it would magically get rid of any black pixels.

Here is the code:
<?php
$w=$_GET['w'];
$h=isset($_GET['h'])?$_GET['h']:$w;    // h est facultatif, =w par défaut
$x=isset($_GET['x'])?$_GET['x']:0;    // x est facultatif, 0 par défaut
$y=isset($_GET['y'])?$_GET['y']:0;    // y est facultatif, 0 par défaut
$filename=$_GET['src'];
header('Content-type: image/png');
header('Content-Disposition: attachment; filename='.$_GET['id']);
$image = imagecreatefrompng($filename); 
$crop = imagecreatetruecolor($w,$h);
$trans = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($crop, $trans);
imagecopy ( $crop, $image, 0, 0, $x, $y, $w, $h );
imagepng($crop);
?>


Any ideas as to why it makes the black pixels transparent also?
--Sole_Wolf
0


User is offline Daniel15 

  • dan.cx
  • Group: Moderators
  • Posts: 3415
  • Joined: 17-April 10
  • LocationMelbourne, Australia
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 28 April 2012 - 10:14 PM (#2)

I'd suggest using ImageMagick - Its interface is a lot cleaner than GD, and it should preserve the transparency. You should be able to run
pecl install imagick 
to install it into your PHP installation.

Try something like:
$image = new Imagick('file.png');
$image->crop($width, $height, $x, $y);
header('Content-type: image/png');
echo $image;


More ImageMagick examples here: http://php.net/manua....examples-1.php
Daniel15! :D
Posted Image

Repeat after me: jQuery is not JavaScript. It is not the answer to every JavaScript-related question. When you have to write some JavaScript, do not instantly react with "Oh, I'll do that with jQuery!"

Spoiler
0


User is offline Sole_Wolf 

  • Group: Members
  • Posts: 303
  • Joined: 24-March 10
  • Expertise:HTML,PHP,Java,SQL

Posted 29 April 2012 - 01:38 PM (#3)

That's strange. My server has Imagick installed on it, but when I used the code that you gave me, I get this error.

Fatal error: Class 'Imagick' not found in /home/....... on line 18


EDIT: Nevermind, I got it working. Everything works great now! :D
--Sole_Wolf
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services