Hello i a have a problem with my site. The thing is that the images take too much space and the site runs slow. A programmer seiad that i need to resize them. How do i do that? i have set the upload file to be under 2mb.

this is the code that returns the image

 echo '<div class="slide'.$i.'" ><img src="'.$final_image.'" class="imgpreview" id="'.$omsg_id.'" rel="'.$msg_id.'"/></div>';

Recommended Answers

All 35 Replies

Hi,
check for GD or Imagick libraries, these are both part of PHP:

Also, when saving the images you should optimize them for the web, or at least reduce their quality, read about this topic here:

Anyway don't look only to the file size, but also to the total of the pixels, in particularly read about getimagesize():

A uploader could serve an apparently small file and crash PHP, for example with the below command in ImageMagick you can create a 2.3MB file with 20000 pixels per side, which uncompressed becomes a 3GB file:

convert -size 20000x20000 xc:white -quality 1% test.jpg

If submitted to a PHP script, this will probably crash because of exhausted memory:

For more information, search Daniweb, there are several threads about image resizing with PHP.

commented: thanks +2

OK. Correct if i am wrong. Imagick is a library right? How may i download it?

ok but if in order to resize images i need to install something on my PC dont all users that use my website have to have GD installed in their computers?

They're libraries that PHP uses to edit the images, so they are installed on your server, where the image is manipulated. Nothing needs to be installed on the client PC.

so all i have to do is to upload this file to my server
http://windows.php.net/downloads/pecl/releases/imagick/3.1.2/
and then retunt this variables?

<?php

/*
    A simple example demonstrate thumbnail creation.
*/ 

/* Create the Imagick object */
$im = new Imagick();

/* Read the image file */
$im->readImage( '/tmp/test.png' );

/* Thumbnail the image ( width 100, preserve dimensions ) */
$im->thumbnailImage( 100, null );

/* Write the thumbail to disk */
$im->writeImage( '/tmp/th_test.png' );

/* Free resources associated to the Imagick object */
$im->destroy();

?>
i said that because i see many dll files

@Simon your web server is a Windows system, correct? If yes, then the dll pack to download is dependant on the PHP version and the web server configuration, i.e. as Apache is operating. Look at this file name:

php_imagick-3.1.2-5.3-nts-vc9-x86.zip
  1. The first part php_imagick-3.1.2 stands for the Imagick version;
  2. the second part 5.3 stands for PHP version;
  3. the third part nts stands for Non-Thread-Safe against ts for Thread-Safe, this is related to Apache multi-processing modules, if PHP works under Apache with mod_php then the package should match the same setup;
  4. The fourth part vc9 is related to Visual Studio version, it's easy: if using PHP 5.3 and 5.4 it will always be vc9, from PHP 5.5+ it's always vc11;
  5. The last segment x86 is the platform, i.e. a 32bit CPU.

Now, in case you still want to use Imagick, the server must be provided with ImageMagick:

However if your server is not provided with Imagick or ImageMagick, rather then trying to install it by yourself, the easiest solution is to use the GD library which most of the times is embedded in PHP.

ok @cereal sorry for your trouble dude but i had a simiral problem. No it uses linux and i will turn on my hosting company... One last question in case they dont help me. Can you send me a link to an example of how to use GD?

Member Avatar for iamthwee

Stay away from imagick it's overkill.

Just simply make sure you have gd2 installed or similar. If you're using ubuntu I'd imagine the default lamp stack installation should have this by default.

i am using linux

Ok, the GD library is available also in the PHP linux distribution, however is you still want to use Imagick: which distro are you using: debian, ubuntu, centos? Do you have access to a linux command line (shell/terminal)? Are you sure ImageMagick and the library for PHP are not already installed?

Setup

To check if ImageMagick is there type this in the command line:

identify -version

It should return something like:

Version: ImageMagick 6.7.7-10 2014-03-06 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP

Regarding Imagick instead, you can check if the extension is enabled through phpinfo() or by using:

echo extension_loaded('imagick') ? 'true':'false';

if the extension is not enabled, you can check if it does exists by typing this in the linux command line:

locate imagick.so

Or, if locate is missing or not updated, use:

find / -name imagick.so 2> /dev/null

Once you are sure about the existence of the file, just enable it by appending this to the php.ini file:

extension=imagick.so
Install

Debian, Ubuntu and other derived distros from Debian use the apt interface to install software, so if you want to install the extension, type:

sudo apt-get install php5-imagick

As suggested in one of my previouses posts. For other distros, like Centos you have to use the rpm interface:

rpm install php5-imagick

If ImageMagick is missing in both cases you should get a notice and a suggest to continue the process by installing the missing packages.

If, for some reason, you don't get it to work please return with detailed information, bye.

commented: thanks +0

ok thanks Cereal. This is what my server has installed

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] => )

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

what is my next step?

this is what the imagemagick.php has

<?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;
?>

so what i do now is include the file (imagemagick.php) to the file where i call the images and write this?

                include('imagemagick.php'); 

                $query1=mysql_query("select image_path from user_uploads where id=".$p );
                while($row=mysql_fetch_array($query1))
                {
                $newdata=$row['image_path'];
                $final_image=$base_url.$path.$newdata;
                $im = new Imagick();
                $im->readImage( $final_image );
                $im->thumbnailImage( 50, 50 );
                $im->writeImage( $base_url.$path.$newdata );
                echo '<a class="show-all-audio" href="#show-all-audio' . $messageid . '" style="cursor:pointer" id="show_all_audio"><img src="'.$im.'" class="small_icon" original-title="'.$message.'" ></a>';
                }

i did that and when i type the url it says on the browser

the image (url) can not be displayed because it has errors

Oh, now I understand, no, you don't have to pass the $im object to display the image you have to use the web server path.

In your previous script there is a problem here:

$final_image=$base_url.$path.$newdata;

Where $base_url is probably your url, correct? If you use this Imagick will not be able to read, nor to write the file to the correct destination.

For example if you web server root is /var/www/domain.tld/ and the source directory is uploads/, and the destination path is thumbs/ then the $final_image path should look like:

$final_image = '/var/www/domain.tld/uploads/'.$filename;

After that, by using $im->writeImage() without the first argument you overwrite the original image defined by $im->readImage($final_image), byt thesetting the argument with a new path you write the new image in a new directory, for example:

$im->writeImage('/var/www/domain.tld/thumbs/'.$filename);

Now, you have a query with which you retrieve the image_path column:

select image_path from user_uploads where id=".$p

What is the content of this column? Path and filename? Can you show us some examples?

To display the new image through the browser, instead you have to use the web server path:

echo '<img src="http://www.domain.tld/thumbs/'.$filename.'" />';

Yes the $newdata is the filename
row in table --> image_path
filename($p) --> 13743039732.jpg

So the directory i save the images is /public_html/uploads/photos
And thats the thing i dont understand. This is my web server root?

$path = "uploads/photos/";
yes this is my url $base_url='http://www.mydomain.com/';

i have created a folder in uploads/thumb right?

and if i am correct

$filename=$row['image_path'];
$final_image=$base_url.$path.$filename;
$im->readImage( $final_image );
$im->thumbnailImage( 50, 50 );
$im->writeImage( $base_url.'uploads/thumbs/'.$filename);

echo '<img src="'.$base_url.'uploads/thumbs/'.$filename.'" />';

so the original file is saved on the uploads/photos if you ever need to return the original size and the thumb (50X50) is saved on the uploads/thumbs directory right?

p.s. thanks man i've trying since the holidays to solve this

one lasting @cereal what about is i want to overwrite the photo (13743039732.jpg) on the uploads/photos in order to save space?

this is a segment of the code i use in order the user to upload the photo

if($size<(2048*2048))
{
    $actual_image_name = time().$uid.".".$ext;
    $tmp = $_FILES['photoimg']['tmp_name'];
    if(move_uploaded_file($tmp, $path.$actual_image_name))
    {
        $data=$Wall->Image_Upload($uid,$actual_image_name);
        $newdata=$Wall->Get_Upload_Image($uid,$actual_image_name);
        if($newdata)
        {
            echo '<img src="'.$path.$actual_image_name.'"  class="preview" id="'.$newdata['id'].'"/>';

        }
    }
$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->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="'.$base_url.'uploads/thumbs/'.$filename.'" class="small_icon" original-title="'.$message.'" ></a>';
                }

Hello, sorry for late reply. So, you're almost there, when you define this:

$final_image='/public_html/uploads/photos/'.$filename;

You have to specify the full path, in relation with the document root, you can do it like this:

$final_image = $_SERVER['DOCUMENT_ROOT'] . '/public_html/uploads/photos/' . $filename;

This will add the missing part, apply the same variable to the write context:

$im->writeImage($_SERVER['DOCUMENT_ROOT'] . '/public_html/uploads/thumbs/' . $filename);

If it does not work this can be due to the permission settings of the destination directory, it needs to be writable by the PHP script.

About document root: http://php.net/manual/en/reserved.variables.server.php

Hope it helps, bye! :)

Thanks man but i still get the same error message:
image cannot be displayed because it contains errors

this is from my error log

PHP Parse error: syntax error, unexpected '.' in /home/ro265852/public_html/imagetest.php on line 25
PHP Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/ro265852/public_html/gd_imagic.php on line 4

PHP Notice: Undefined index: to in /home/ro265852/public_html/send_mail.php on line 2

and i dont see any imagetest.php file on my server

Regarding this error:

PHP Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/ro265852/public_html/gd_imagic.php on line 4

Can you show full source of gd_imagic.php?

By the way, from the log you can see the document root:

/home/ro265852/public_html/

So, it's possible that my previous suggestion was wrong, because of the repetition of the public_html/ segment, the correct versions would be:

$src = $_SERVER['DOCUMENT_ROOT'] . '/uploads/photos/' . $filename;
$dst = $_SERVER['DOCUMENT_ROOT'] . '/uploads/thumbs/' . $filename;

in the gd_imagic.php its just this....

<?php
echo "GD module info</br>";

print_r(gd_info());

echo "</br></br>ImageMagick module info</br>";

print_r(imagick::getVersion());

?>

this is the full code i use

include('imagemagick.php'); 
                $query1=mysql_query("select image_path from user_uploads where id=".$p );
                while($row=mysql_fetch_array($query1))
                {
                            $filename=$row['image_path'];
                            $final_image = $_SERVER['DOCUMENT_ROOT'] . '/uploads/photos/' . $filename;
                            $im->readImage( $final_image );
                            $im->thumbnailImage( 50, 50 );
                            $im->writeImage($_SERVER['DOCUMENT_ROOT'] . '/uploads/thumbs/' . $filename);
                        echo '<a class="show-all-audio" href="#show-all-audio' . $messageid . '" style="cursor:pointer" id="show_all_audio"><img src="'.$base_url.'uploads/thumbs/'.$filename.'" class="small_icon" original-title="'.$message.'" ></a>';
                }
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.