I have a problem with resizing images with imagemagick
this is the info

ImageMagick module info
Array ( [versionNumber] => 1672 [versionString] => ImageMagick 6.8.8-7 Q16 x86_64 2015-01-27 http://www.imagemagick.org )

this is the imagemagick.php

<?php
$im = new Imagick();
$im->newPseudoImage(50, 50, "gradient:red-black");
$draw = new ImagickDraw();
$draw->pushPattern('gradient', 0, 0, 50, 50);
$draw->composite(Imagick::COMPOSITE_OVER, 0, 0, 50, 50, $im);
$draw->popPattern();
$draw->setFillPatternURL('#gradient');
$draw->setFontSize(52);
$draw->annotation(20, 50, "imagemagick");
$canvas = new Imagick();
$canvas->newImage(350, 70, "white");
$canvas->drawImage($draw);
$canvas->borderImage('black', 1, 1);
$canvas->setImageFormat('png');
header("Content-Type: image/png");
echo $canvas;
?>

this is the code i use to return the image

$query1=mysql_query("select image_path from user_uploads where id=".$p );
                while($row=mysql_fetch_array($query1))
                {
                    include('imagemagick.php'); 

                            $filename=$row['image_path'];
                            $final_image='/public_html/uploads/photos/'.$filename;
                            $im = new Imagick();
                            $im->readImage($final_image);
                            $im->thumbnailImage( 50, 50 );
                            $im->writeImage('/public_html/uploads/thumbs/'.$filename);
                        echo '<a class="show-all-audio" href="#show-all-audio' . $messageid . '" style="cursor:pointer" id="show_all_audio"><img src="http://www.rovespier.com/uploads/thumbs/'.$filename.'" class="small_icon" original-title="'.$message.'" ></a>';
                }

all i get is an error page that says login.png cant be shown because it has errors on my login page http://www.rovespier.com/login.php

Recommended Answers

All 15 Replies

Can you test it on non secured page and check results,
some development environment maybe?

this is the info...

GD module info
Array ( [GD Version] => bundled (2.1.0 compatible) [FreeType Support] => 1 [FreeType Linkage] => with freetype [T1Lib Support] => 1 [GIF Read Support] => 1 [GIF Create Support] => 1 [JPEG Support] => 1 [PNG Support] => 1 [WBMP Support] => 1 [XPM Support] => 1 [XBM Support] => 1 [JIS-mapped Japanese Font Support] => )

if it is installed can you send me an example of how to use it?

locally (wampserver) it doesnt shows the photos at all

Member Avatar for iamthwee

I can confirm you have gd2 installed which mean resizing works.

As for resources google is your friend.

Any issues post em here and we'll walk you through it.

this one seems good but i dont know how to save...

jstricks.com/php-function-resize-image-using-gd/

this is the code i use

$query1=mysql_query("select image_path from user_uploads where id=".$p );
while($row=mysql_fetch_array($query1))
{
    $filename= $base_url.$path.$row['image_path'];
    resizeMyImage($filename, $Deimage, 50, 50);

    echo '<img src="'.$Deimage.'" ></a>';
    //or
    echo '<img src="'.$filename.'" ></a>';
}

filename retuns the original version Deimage retuns nothing

this is the function any help???

function resizeMyImage($file, $destination, $w, $h) {
    //Get the original image dimensions + type
    list($source_width, $source_height, $source_type) = getimagesize($file);

    //Figure out if we need to create a new JPG, PNG or GIF
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    if ($ext == "jpg" || $ext == "jpeg") {
        $source_gdim=imagecreatefromjpeg($file);
    } elseif ($ext == "png") {
        $source_gdim=imagecreatefrompng($file);
    } elseif ($ext == "gif") {
        $source_gdim=imagecreatefromgif($file);
    } else {
        //Invalid file type? Return.
        return;
    }

    //If a width is supplied, but height is false, then we need to resize by width instead of cropping
    if ($w && !$h) {
        $ratio = $w / $source_width;
        $temp_width = $w;
        $temp_height = $source_height * $ratio;

        $desired_gdim = imagecreatetruecolor($temp_width, $temp_height);
        imagecopyresampled(
            $desired_gdim,
            $source_gdim,
            0, 0,
            0, 0,
            $temp_width, $temp_height,
            $source_width, $source_height
        );
    } else {
        $source_aspect_ratio = $source_width / $source_height;
        $desired_aspect_ratio = $w / $h;

        if ($source_aspect_ratio > $desired_aspect_ratio) {
            /*
             * Triggered when source image is wider
             */
            $temp_height = $h;
            $temp_width = ( int ) ($h * $source_aspect_ratio);
        } else {
            /*
             * Triggered otherwise (i.e. source image is similar or taller)
             */
            $temp_width = $w;
            $temp_height = ( int ) ($w / $source_aspect_ratio);
        }

        /*
         * Resize the image into a temporary GD image
         */

        $temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
        imagecopyresampled(
            $temp_gdim,
            $source_gdim,
            0, 0,
            0, 0,
            $temp_width, $temp_height,
            $source_width, $source_height
        );

        /*
         * Copy cropped region from temporary image into the desired GD image
         */

        $x0 = ($temp_width - $w) / 2;
        $y0 = ($temp_height - $h) / 2;
        $desired_gdim = imagecreatetruecolor($w, $h);
        imagecopy(
            $desired_gdim,
            $temp_gdim,
            0, 0,
            $x0, $y0,
            $w, $h
        );
    }

    /*
     * Render the image
     * Alternatively, you can save the image in file-system or database
     */

    if ($ext == "jpg" || $ext == "jpeg") {
        ImageJpeg($desired_gdim,$destination,100);
    } elseif ($ext == "png") {
        ImagePng($desired_gdim,$destination);
    } elseif ($ext == "gif") {
        ImageGif($desired_gdim,$destination);
    } else {
        return;
    }

    ImageDestroy ($desired_gdim);
}
Member Avatar for iamthwee

Not sure what you're trying to do but just do a simple upload image and crop test... Once you confirmed that as successful then integrate it into your current code.

Simples... I personally, don't use native php anymore, I do all my queries and image crop via a suitable php framework, it's worth learning and it is bags more readable.

As an example. See codeigniter implementation of cropping.

https://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

Pretty easy to understand right?

Wrong.... You are not saying anything i dont know so you dont help me figure out whats wrong not to do it for me but to tell whats wrong as @Cereal and many more are doing in here. If anyone answered like you do (google is your friend as things like that) then there would be no purpose for sites like that Right?

Member Avatar for iamthwee

OK I see, where this is going, yes you are correct simonloa, I have been a bit vauge, but the truth is every language, every application starts from a simple premise:

Get the basics right then we can move from there. Any language begins from the basics... get a simple upload and resize image working with php, then with can worry about the rest.

Your code you have submitted is specific to your database and your webiste. It has nothing to do with generalisations.

I said get a simple upload and resize working with gd2, which as I still maintain is more easy than using imagick which is more unlikely to be standard in most web hosting sites.

Imagick is overkill, why Cereal directed you to using this is his personal perference. Cereal is someone I respect, I've read his posts and I know his opinion is more likely than NOT to be validated.

However, on this particular, instance, I believe using a standard Gd library to resize an image is much simpler and more likely to be supported across atypical web hosting companies than imagick.

All I'm offering is my experiences nothing more nothing less. Take it for what it is.

Member Avatar for iamthwee

btw I've not looked at your code properly, but my gut is telling me this line is somehow wrong...

$query1=mysql_query("select image_path from user_uploads where id=".$p );

I'm guessing it should be

$query1=mysql_query("select image_path from user_uploads where id='$p'" );

But seeing as this has nothing to do with php image resizing and simple db querying you it doesn't really belong in the same thread.

Thanks.

Member Avatar for iamthwee

If it is not that which it might not be, test to see if your paths are correct and your folder has write permissions, thats about the best I can say without running the script.

ok @iamthwee. the paths are ok... the thing is that i used imagemagick and it worked except that the images returned blured. Another said that gd is better but didnt understand how it works. Can you place an example here?

Member Avatar for iamthwee

http://stackoverflow.com/questions/8319203/imagemagick-vs-gd-which-is-faster-less-resource-intensive-and-produces-better

If you want more features and quality opt for imagemagick. The fact that your images are returning blurred, is simply due to you missing a parameter or a setting is not correctly set.

However, I said the overwhelming reason GD is preferred is that not all hosting companies support this. This is something you need to consider, you may have your own VPS, so this might not be an issue.

In terms of a very very simple usage example.

http://stackoverflow.com/questions/12571276/copy-and-resize-image-with-php-gd-library

Once again, as long as you have set file permission to write to the folder it should be as simple as that.

Still if you have the time, which you might not, migrating your code to an MVC framework such as codeigniter or laravel or cakephp, whichever you fancy is well worth it in the long run.

As per usual, start with a stand alone script. Upload one image, see if it creates a resized image. Only then start integrating it with your actual code and db queries.

the file permissions are ok... i been working on on it for two weeks... i found imagemagick better... plus i dont got to use GD to resize images... i used everything i know. but with imagemagick i resized the images but they return blur -i mean with low quality- and i dont see to fix that... although i google it too

Member Avatar for iamthwee

Try setting the quality to 100% and see if that fixes the blur. If you are resizing the image to make it bigger it will be blurred. Smaller it won't.

I can't help, I don't use IM. I just use GD2 in codeigniter.

Better still attach an image you are trying to resize and the dimensions you want it cropped to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.