2

This is my plan:

Upon clicking the submit button. The image must appear to be sheared.

But on my case, it isn't appearing as planned.

This is my code:(just ignore the // after the //this is where...

<?php

 if(isset($_POST['Submit'])){

    // loop through the uploaded files
    foreach ($_FILES as $key => $value) {

        $image_tmp = $value['tmp_name'];
        $image_type=$value['type'];
        $image = $value['name'];
        $image_file = "{$UPLOADDIR}{$image}";

        //check if there's existing file name
        if ($image  != 0){
            echo 'File Already Exists!';
        } else {
            // move the file to the permanent location
            if(move_uploaded_file($image_tmp,$image_file)){
                // this is where the shearing and displaying part goes

                $shear = new Imagick($image_file);
                $shear ->shearImage('grey', 10, 5);
                $shear ->writeImage($UPLOADDIR.'sheared-'.$image);
                echo "<div style='float:left;margin-right:10px'>
                         <img src='{$shear}' alt='file not found' /></br>
                     </div>";
            } else {
                echo "<h1>image file upload failed, image too big after compression</h1>";
            }
        }
    } // end foreach
}

?>

Thank you so much for your response

BrightIntelDusk
  • 4,577
  • 2
  • 25
  • 34
user3335903
  • 1,475
  • 4
  • 14
  • 13

1 Answers1

0

Just a disclaimer I've never used Imagick though I'm very familiar with PHP.

It looks like you are attempting to echo out an instance of the class Imagick. This would work correctly if the implementation of Imagick had a magic method for __toString(). From my review of the PHP manual it appears that Imagick does not have this method.

When outputting a string into an HTML image tag you would need the src attributes value to be either a specific directory and file name or base 64 encoded image.

One option you can use is to get the file blob and save the image using file_put_contents(). In the image src you would place the name of the file.

 if(move_uploaded_file($image_tmp,$image_file)) {


    $shear = new Imagick($image_file);
    $shear->shearImage('grey', 10, 5);
    $shear->writeImage($UPLOADDIR.'sheared-'.$image);
    $data = $shear->getImageBlob();

    file_put_contents('test.png', $data); 

    echo "<div style='float:left;margin-right:10px'>
           <img src='test.png' alt='file not found' /></br>
        </div>";

}

Another possibility is that you could assign the image files path in a separate variable and output that.

 if(move_uploaded_file($image_tmp,$image_file)) {

    $shear = new Imagick($image_file);
    $shear->shearImage('grey', 10, 5);
    $img_path = $UPLOADDIR.'sheared-'.$image;
    $shear->writeImage($img_path);

    echo "<div style='float:left;margin-right:10px'>
           <img src='{$img_path}' alt='file not found' /></br>
        </div>";

}

A third option is that you could base 64 encode the blob and display it directly instead of writing the file.

 if(move_uploaded_file($image_tmp,$image_file)) {

    $shear = new Imagick($image_file);
    $shear->shearImage('grey', 10, 5);
    $img_path = $UPLOADDIR.'sheared-'.$image;
    $shear->writeImage($img_path);
    $data = base64_encode($shear->getImageBlob());

    echo "<div style='float:left;margin-right:10px'>
           <img src='data:image/jpg;base64,{$data}' alt='file not found' /></br>
        </div>";

}

Take a look at this question for more information PHP-Imagemagick image display

Community
  • 1
  • 1
BrightIntelDusk
  • 4,577
  • 2
  • 25
  • 34
  • I have a variable that contains the upload directory, and the file name written as `$UPLOADDIR = "images/";` and `$image = $value['name'];` In my case, I seperated them so I could easily notice if the file to be uploaded was sheared or not, so in the folder, there will be 2 files containing the original image and the sheared one. – user3335903 Feb 27 '14 at 04:30
  • So you have a variable for the original image files path. You need a variable for the sheared files name path. Then in your img tag your src attribute will have the sheared files path variable as its value. – BrightIntelDusk Feb 27 '14 at 04:35
  • Can you check if this is correct? `$editedShear = ($UPLOADDIR.'sheared-'.$image);` – user3335903 Feb 27 '14 at 04:37
  • Yes that is accurate. – BrightIntelDusk Feb 27 '14 at 04:39
  • I got my expected output. Thank you. :) – user3335903 Feb 27 '14 at 04:57