tags:

views:

59

answers:

5

Read the bottom first, where the most current information is. Most of this is old information (though you might need it).

I'm starting on making a PHP function that morphs/deforms an image you give it. I need to make a black background for starting, so this is what I have so far: (not anymore)

function morph($img) {

    list($width, $height) = getimagesize($img);
    //$width = $size[0];
    //$height = $size[1];
    $tempImg = imagecreatetruecolor($width, $height);

    //Create the image background
    imagefilledrectangle($tempImg, 0, 0, $width, $height, imagecolorallocate($tempImg, 0, 0, 0));
    return $tempImg;
}

But all it gives me is a broken image, at least from the browser's perspective. What could be wrong? Thanks in advance.

EDIT: An alternative to fixing this, which a lot of people are having trouble with, would be to simply make a method that creates a true color image the same size as an imag from an argument that is filled with black. I'll do my best to do this, but it'd be great if you could help too.

This is my current attempt:

function morph($img) {

    list($width, $height) = getimagesize($img);
    //$width = $size[0];
    //$height = $size[1];
        $tempImg = imagecreatetruecolor($width, $height);

        //Create the image background
        imagefilledrectangle($tempImg, 0, 0, $width, $height, imagecolorallocate($tempImg, 0, 0, 0));
        return $tempImg;
}

Same result.

EDIT 2: I have found the source of the problem! After snooping around the result, I found that the dimensions imagecreatetruecolor() was receiving were invalid. I'm doing something wrong in getimagesize() and the array I'm getting. I need to figure out what.

EDIT 3: The problem is in getimagesize(). It says no such file or directory at at line 13 (the line with getimagesize). What's wrong? This was the problem all along.

A: 

Have you set the mime type of the file you're outputting to image/png ?

Edit: for example: header("Content-type: image/png");

I had the same problem once until I added that.

DrDipshit
Yes, I have. Remember this is part of a larger program, so I've already gotten everything else to work.
Stuart
+1  A: 

is $width and $heigth actually int?

Acron
Well, I've done the same rectangle drawing method to the image I'm putting into it... So I think so.
Stuart
A: 

Check the error logs or run your script without setting the header content-type. You will see the error messages that is causing your script to fail. In this case you will most probably get something like '$tempImg is not a resource'.

Meloth
It still gave me a broken image... Why would it do that? Besides, when there's an error it always never gives an image, it throws an exception and gives me it in text form.
Stuart
A: 

You have to specify the kind of file are you giving to the client. header("Content-type: image/something"); Where something is the kind of image. No other print, echo or html has to be in the page or the image will be broken.

Charlie
I already answered that in another comment; I have gotten the script to work, now I am simply changing an image with a function. Why would I post a question specifically about a method if the rest of the code didn't work in the first place? Anyways, this whole thing is for a CAPTCHA project I'm working on- certainly not for-profit and I probably won't even release it... But the morph method will twist the letters around that it creates. I have the rest of the code working, now I just want to twist the letters around before it ships away the image.
Stuart
+1  A: 

EDIT 3: The problem is in getimagesize(). It says no such file or directory at at line 13 (the line with getimagesize). What's wrong?

Well there is no way we can tell for sure whats going wrong with the code you posted only that you are passing an invalid argument into the function.

My best guess is that $img was previously created in RAM using imagecreate(), which returns a resouce handle.

getimagesize($img) expects a file path to be passed in as the parameter, so passing a handle in will b0rk it.

if this is the problem then you should be able to fix it by passing the dimensions into the function and removing the line with getimagesize() on it.

This was the problem all along.

Not quite. pr0wl pointed out your original problem, which you removed and replaced with another problem.

DrDipshit
Thank you. Perfect.
Stuart