开发者

PHP image editing

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 = $开发者_如何学Csize[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.


is $width and $heigth actually int?


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.


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.


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'.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜