-2

When I run this code I get the following error:

The image "http://siteprevue.net/flipit.php" cannot be displayed because it contains errors.

I'm thinking... of course it can't be displayed, it's a url not an image... duh?!

Everything executes fine, I just get this black page that thinks the url is an image.

This happens on (some) other GD2 coded pages as well, but not all.

Does anyone have any idea what's happening?

<?php
$src = '../../Uploads/Gallery/drafting_site_bg_200.jpg';
$new_img = '../../Uploads/Gallery/copy_bg_200.jpg';

$image = imagecreatefromjpeg($src);
$image = flip($image,1,0); // flips horizontal
//$image = flip($image,0,1); // flips vertical
//$image = flip($image,1,1); // flips both

header("Content-type: image/jpeg");
imagejpeg($image, $new_img, 80);
imagedestroy($image);

function flip($i,$h=1,$v=0) {
$width = imagesx($i);
$height = imagesy($i);
$temp = imagecreatetruecolor($width,$height);
imagecopy($temp,$i,0,0,0,0,$width,$height);
if ($h==1) {
for ($x=0 ; $x<$width ; $x++) {
imagecopy($i, $temp, $width-$x-1, 0, $x, 0, 1, $height);
}
imagecopy($temp,$i,0,0,0,0,$width,$height);
}
if($v==1) {
for ($x=0; $x<$height ; $x++) {
imagecopy($i, $temp, 0, $height-$x-1, 0, $x, $width, 1);
}
}
return $i;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org   /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
Kuya
  • 7,280
  • 4
  • 19
  • 31

2 Answers2

0

First you dont need html, because your output is an jpeg. You missed the @ on @imagecreatefromjpeg. But your problem is the output file name which doesnt work together with header. The image will created sucessfully, but you get an error.

imagejpeg filename

The path to save the file to. If not set or NULL, 
the raw image stream will be outputted directly.

You can either save the file oder print it with header. So you should insert null. This works for me, but it wont store you changes:

<?php
$src = '../../Uploads/Gallery/drafting_site_bg_200.jpg';

$image = imagecreatefromjpeg($src);
$image = flip($image,1,0); // flips horizontal
//$image = flip($image,0,1); // flips vertical
//$image = flip($image,1,1); // flips both

header("Content-type: image/jpeg");
imagejpeg($image, null, 80);
imagedestroy($image);

function flip($i,$h=1,$v=0) {
    $width = imagesx($i);
    $height = imagesy($i);
    $temp = imagecreatetruecolor($width,$height);
    imagecopy($temp,$i,0,0,0,0,$width,$height);
    if ($h==1) {
        for ($x=0 ; $x<$width ; $x++) {
            imagecopy($i, $temp, $width-$x-1, 0, $x, 0, 1, $height);
        }
        imagecopy($temp,$i,0,0,0,0,$width,$height);
    }
    if($v==1) {
        for ($x=0; $x<$height ; $x++) {
            imagecopy($i, $temp, 0, $height-$x-1, 0, $x, $width, 1);
        }
    }
    return $i;
}

?>
JaMaBing
  • 1,051
  • 14
  • 32
  • I need to store the changes. I want to save the file, not display it. This will eventually be part of an image upload form. I'm not new to coding, but I am new to GDlib. Thanks :) – Kuya Sep 04 '14 at 14:05
0

I'm thinking... of course it can't be displayed, it's a url not an image... duh?!

With this line

header("Content-type: image/jpeg");

you are telling the browser that the data he receives after this was that of a JPEG image – so if you’re not intending to send image data after that, that line has no place being there.

Simply remove it, so that PHP will send its default Content-Type text/html again.

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • Thanks @CBroe... I was in the midst of posting "my" answer while you posted this. You pointed me in the right direction, so... points for you. Much appreciated. Maybe you can also help me with a related problem. http://stackoverflow.com/questions/25664985/gd2-calling-a-function-after-form-submit – Kuya Sep 04 '14 at 14:33