Jump to content

PHP Image Manipulation Problems.


jordz

Recommended Posts

Hey all,

 

I've been having an issue with using two scripts, one is an image manipulation class that does some watermarking for me and has a variety of different functions. Once I've processed the image from that class It comes out in a raw format (requiring a header()). I built another script to add 25 pixels to the bottom off the image but for the life of me can't seem to take the image from the raw format and manipulate it. Does anyone know how I could take the raw image and do something like imagecreatefromjpeg() to it?

 

 

here's my code:

include('class.upload.php'); //get the uploader class
header('Content-Type: image/jpeg'); //image header

$filename = 'test.jpg'; //source image file

$handle = new upload($filename);
if ($handle->uploaded) {
   // process the image
        $handle->file_new_name_body   = 'image_resized';
$handle->image_watermark = 'wm2.png';
$handle->image_watermark_position = 'RM';

        //outputs the image as raw format.
        $handle->process();
}


// I can't do $image = $handle->process() then feed it through...
// My script starts here and $image should be what comes out of the $handle...

         //*********** This is where I'm stuck, any help?**********
         $source = imagecreatefromjpeg($image);
         
         list($width, $height) = getimagesize($image);
 $thumb = imagecreatetruecolor($width, $height+25);
        // Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $width, $height, $width, $height);


 

Any help would be much appreciated thanks.

 

Jordan

Link to comment
Share on other sites

You asked how can you take a raw image and do imagecreatefromjpeg() to it?

 

Well, what I posted are bits of virtually what imagecreatefromjpeg() can do.

 

If you'd like a better imagecreatefromjpeg() example then read below,

 

<?php
function LoadJpeg($imgname)
{
    /* Attempt to open */
    $im = @imagecreatefromjpeg($imgname);

    /* See if it failed */
    if(!$im)
    {
        /* Create a black image */
        $im  = imagecreatetruecolor(150, 30);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);

        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

        /* Output an error message */
        imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }

    return $im;
}

header('Content-Type: image/jpeg');

$img = LoadJpeg('bogus.image');

imagejpeg($img);
imagedestroy($img);
?>

Link to comment
Share on other sites

I'm still failing to see how this works... Sorry If Im a little thick.

 

$handle->process() outputs the image in raw code, how exactly would I go about taking that raw code and manipulating the image again?

 

 

Link to comment
Share on other sites

Basically, you will have to do what I coded yesterday :D You need to write the data to a temporary file and then use that temporary file on imagecreatefromjpeg. Then, when all is done, delete the temporary file. I use @ to compress errors just because I am using this in a cron script and I output JSON (No idea why, I just do 8)). Example:

 

<?php

$fileName = "button.jpg";
$rawData = "xxx"; // This is where you put $handle->process();
$tempDir = "temp/"; // CHMOD this to 0777.
$realDir = "images/"; // CHMOD this to 0777 as well.

$fp = @fopen($tempDir.$fileName.".tmp", "wb"); // 'b' is to use binary (Without the 'b' tag, I had loads of corruption issues and I finally figured this out).
if($fp) {
    fwrite($fp, $rawData); 
    fclose($fp);
} else {
    die("Could not open a temporary file (Check directory has CHMOD 0777)");
}
if(!$src = @imagecreatefromjpeg($tempDir.$fileName.".tmp")) {
    die("Could not open the temporary file and create a new jpeg object from it.");
}
// Carry out operations...
imagejpeg($src, $realDir.$fileName, 100); // Save the file to a directory.
imagedestroy($src);
@unlink($tempDir.$fileName.".tmp"); // Delete the temporary file.

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.