Can i change image file size with php? - php

I made a small function to upload images then show them on my website.
My question is now, can I change the file size of a image, on same time as I upload it?
Take facebook as example. Even large images has a size on like 65kb.
If so, witch function should I use?
Thanks,

use the imagecopyresampled function:
$source_image = imagecreatefromjpeg("youtimagejpg");
$src_w= imagesx($source_image);
$src_h= imagesy($source_image);
$dest_image = imagecreatetruecolor(100, 100); //targeted width and height
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, 100, 100, $source_w, $source_h);
imagejpeg($dest_image,NULL,80);
//Replace null by the path if you want to save on the server, otherwise the image will be sent to the client intead.
If you want to reduce the size of the image without resizing it; use the imagejpeg function (with a low quality factor)

There are next ways to change image size:
Convert to another image format.
Save image with lossy compression (http://en.wikipedia.org/wiki/Lossy_compression)
Resample image with another width & height (http://php.net/manual/en/function.imagecopyresampled.php)

Related

Convert any type of an image to PNG and add to PNG

I am trying to create an employee poster for a website that I develop. My goal is to take an image from a directory on the server of any file type (.png, .gif, .jpeg, etc.) and copy it onto another generate imaged that will then be outputted to the browser.
The problem is that I use:
$final_image = imagecreatefrompng("large_background.png");
for making the final image and for some reason if I add profile images with the type jpeg's, gif's, etc, (any type that isn't a jpeg) it doesn't work. The images never show up in the output. However, if I use png's it does work.
To solve this problem I tried converting the image to a png and then creating a png from it as shown in the code below. Unfortunately it doesn't work. Profile images still do not show up on the background.
// get image from database
$image_from_database = could be a .png, .jpeg, .gif, etc.
// get the image from the profile images directory
$path = "profile_images/".$image_from_database;
// create a png out of the image
$image = imagecreatefrompng(imagepng($path));
// add the $image to my larger $final_image (which is a png)
imagecopy($final_image, $image, $x, $y, 0,0, $height, $width);
imagepng($final_image, $ouput_url);
...
Can anybody tell me why this won't work? My profile images do not show up in the output of the final image.
My questions,
Is this line imagecreatefrompng(imagepng(...)); even possible? Essentially I want to convert an image of any type into a png and then create a png from it.
I'm just running a few local tests... The following works:
$src = imagecreatefromgif('test.gif');
$dest = imagecreatefrompng('test.png');
imagecopy($dest, $src, 0, 0, 0, 0, 100, 100);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($src);
imagedestroy($dest);
So does this:
$src = imagecreatefromstring(file_get_contents('test.gif'));
If you're still having trouble after trying the latter example, please update your question. The actual images you're using would be helpful, in addition to a functional code example.
After you read an image with a imagecreatefrom* function, it doesn't matter what format the original was.
imagecreatefrom* functions return an image resource. When the image is loaded you are using internal representation of the images and not PNG, JPEG or GIF images.
If the images are successfully loaded imagecopy should have no problems with them.
This code uses images in different formats and works without a problem:
$img = imagecreatefrompng('bg.png');
$png_img = imagecreatefrompng('img.png');
$jpeg_img = imagecreatefromjpeg('img.jpeg');
$gif_img = imagecreatefromgif('img.gif');
/* or use this, so you don't need to figure out which imagecreatefrom* function to use
$img = imagecreatefromstring(file_get_contents('bg.png'));
$png_img = imagecreatefromstring(file_get_contents('img.png'));
$jpeg_img = imagecreatefromstring(file_get_contents('img.jpeg'));
$gif_img = imagecreatefromstring(file_get_contents('img.gif'));
*/
imagecopyresampled($img, $png_img, 10, 10, 0,0, 100, 100, 200, 200);
imagecopyresampled($img, $jpeg_img, 120, 10, 0,0, 100, 100, 200, 200);
imagecopyresampled($img, $gif_img, 230, 10, 0,0, 100, 100, 200, 200);
header('Content-Type: image/png');
imagepng($img);
Your example
$image = imagecreatefrompng(imagepng($path));
is wrong.
imagepng is used to output an image resource as a PNG image. If you provide a path as a second argument a PNG image file is created, otherwise it's printed to the output like echo does.
What imagepng actually returns is a boolean, indicating, if the output was successful.
You are then passing that boolean to imagecreatefrompng which expects a filepath. This is obviously wrong.
I suspect you have a problem with loading images.
imagecreatefrom* functions return FALSE on failure and you should check, if you have any problems with that.
Maybe your image paths are relative to doc root and your working directory is different.
Or you have permission problem.
Or your images are just missing.
It's impossible to tell from your question.

How can I resize an image resource in PHP?

I have a script that creates an image whose height depends on the depth of a recursive tree. Precalculating the height of the image would be a pain, because I would need to run a recursive function twice. Thus, I wonder if it is possible to resize the image from inside my only recursive function. Long story short, I need to resize an image resource, is it possible? For instance, how can I make the following code work? Thanks!
//Create the image
$image = imagecreatetruecolor(800, 100);
//Change the image height from 100 to 1000 pixels
imagecopyresized($image, $image, 0, 0, 0, 0, 800, 1000, 800, 100);
//Set the header
header('Content-Type: image/png');
//Output the image
imagepng($image);
//Destroy the image
imagedestroy($image);
Your code is almost perfect, except you aren't giving it the source image to work with. You need $source = imagecreatefrompng("someimage.png");, and then in your imagecopyresize set the source image argument to $source.

Resizing image with PHP

I used a very simple code to resize image with PHP; but surprisingly it does not work for some images. The problem should be associated with imagecreatefromjpeg(), as it will generate a black image (which is of the background image).
$picture="test5.jpg";
$url="http://www.pokerpurist.com/uploadedImages/bettingpro/NewsImages/TN98553_Perla-Beltran.jpg";
list($width, $height) = getimagesize($url);
$new_height = $height / $width * 400;
$image_p = imagecreatetruecolor(400, $new_height);
$image = imagecreatefromjpeg($url);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 400, $new_height, $width, $height);
imagejpeg($image_p, $picture);
echo "<img src='$picture' />";
This problem happens offen, and I included an example image. What is the problem with these images leading to this problem? It seems to be a normal JPG image.
By the way, is it the simplest and most efficient way to resize image with PHP/GD2?
Your example image is a PNG, not JPEG. You probably need to put some detection code in place...
Edit: exif-imagetype or ImageMagick might be of some use.
#By the way, is it the simplest and most efficient way to resize image with PHP/GD2?
Use Asido: PHP Image Processing Solution
Asido supports the following features:
pluggable drivers for GD2 (php_gd2), MagickWand (php_magickwand),
ImageMagick extension (php_imagick) as well as ImageMagick shell
commands
"hack" drivers: workarounds for certain disabilities of a particular driver by using some of the other functionality provided
by the environment
various resize functionality: proportional resize, resize only by width or height, stretch resize, fit resize, frame resize
watermark images, including tiling watermark and automatic scaling of large watermarks
rotate images
copy images onto one another
crop images
grayscale images
convert images between different filetypes
If you can't access Asido website, you can download Asido from SourceForge.net

Merge images in PHP - GIF and JPG

I am trying to merge two images - a GIF image with a smaller JPG image. The output should be GIF.
The issue is that GIF image colors remain correct, but the colors of the JPG image are altered.
The GIF image has only 256 colors (8-bit), but is there a way to make the merged image to be a true-color resource which later can be converted to a 8-bit GIF for output?
Issue solved.
I updated the code. Here is the solution which works fine:
<?php
header('Content-Type: image/gif');
$gif_address = 'file.gif';
$jpg_address = 'file.jpg';
$image1 = imagecreatefromgif($gif_address);
$image2 = imagecreatefromjpeg($jpg_address);
$merged_image = imagecreatetruecolor(800, 800);
imagecopymerge($merged_image, $image1, 0, 0, 0, 0, 800, 800, 100);
imagecopymerge($merged_image, $image2, 0, 0, 0, 0, 500, 500, 100);
imagegif($merged_image);
imagedestroy($image1);
imagedestroy($image2);
imagedestroy($merged_image);
?>
From your explanation (some code would help), i would hazard a guess you are merging the jpeg onto the gif. Id say the easiest way is to use imageCreateTrueColor to create a new image the size you need then use imagecopy to copy the GIF into this new image. Merge the jpg onto this and then at a later date you can covert the true colour image to a gif.
If im missing something a bit of example code of what you are currently doing might help.

dynamic image resize using php

I have an image which i am going to be using as a background image and will be pulling some other images from the database that i want to show within this image. So if i am pulling only 1 image, i want the bottom part of the background image to close after the first image, if there are multiple images then i want it close after those images are shown. The problem with not using separate images is that the borders of the images have a design format and i cannot show them separately.
Take a look at this image . The design format of the right and left borders are more complicated than that to just crop them and use them. Any suggestions if there is any dynamic image resizing thing?
Yes there is. Look at the imageXXXX functions; the ones you are particularly interested in are imagecreate, imagecreatetruecolor, imagecreatefrompng, imagecopyresampled, imagecopyresized, and imagepng (assuming you're dealing with PNG images - there's similar load / save functions for jpeg, gif, and a few other formats).
You should try using the GD extension for PHP, especially have a look at imagecopyresized(). This allows you to do some basic image conversion and manipulation very easily.
A basic example that takes two GET parameters, resizes our myImage.jpg image and outputs it as a PNG image:
<?php
// width and height
$w = $_GET['w'];
$h = $_GET['h'];
// load image
$image = imagecreatefromjpeg('myImage.jpg');
// create a new image resource for storing the resized image
$resized = imagecreatetruecolor($w, $h);
// copy the image
imagecopyresized($resized, $image, 0, 0, 0, 0, $w, $h, imagesx($image), imagesy($image));
// output the image as PNG
header('Content-type: image/png');
imagepng($resized);
Have you tried PHPThumb? I used this class often and its pretty clean and lightweight. I used it here.

Categories