PHP displaying JPG image from PNG source on server? - php

Is it possible to have png images on my server but have a php script that converts them to jpg (and compresses them) and caches them when viewed?

You can use this script for convert PNG image into JPEG image.
$input_file = "test.png";
$output_file = "test.jpg";
$input = imagecreatefrompng($input_file);
list($width, $height) = getimagesize($input_file);
$output = imagecreatetruecolor($width, $height);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file);
You can go through this #Alexandre Jasmin Answer.

yes it is possible, you can google it using phrase "png convert to jpg php"
Use PHP to convert PNG to JPG with compression?

Related

PHP resize image from FTP

I need resize Image from FTP like /images/1_2.jpg. I need to load it to imagecreatefromjpeg();. Now my script looks like this.
$file = "images/1_2.jpg";
$imagesize = getimagesize($file);
$img = imagecreatefromjpeg($file);
$width = 70;
$height = 100;
$img2 = imagecreatetruecolor($width, $height);
imagecopyresampled($img2, $img, 0, 0, 0, 0, $width, $height, $imagesize[0], $imagesize[1]);
imagejpeg($img2, "images_mini/1_2.jpg");
My problem is that images don't upload to FTP, its shows me everything is ok but i can't find images on FTP.
Sorry I have it, just only bad converted variable, i using cross languages now for application so it is dificult to check everythink. Thank you anyway.

Convert image format PNG to JPEG without saving to disk - PHP

I am taking a PNG image from a url as below.
I want to convert the PNG image to JPEG without saving disk with PHP.
Finally I want to assign JPEG image to $content_jpg variable.
$url = 'http://www.example.com/image.png';
$content_png = file_get_contents($url);
$content_jpg=;
Simplified answer is,
// PNG image url
$url = 'http://www.example.com/image.png';
// Create image from web image url
$image = imagecreatefrompng($url);
// Start output buffer
ob_start();
// Convert image
imagejpeg($image, NULL,100);
imagedestroy($image);
// Assign JPEG image content from output buffer
$content_jpg = ob_get_clean();
You want to use the gd library for this. Here's an example which will take a png image and output a jpeg one. If the image is transparent, the transparency will be rendered as white instead.
<?php
$file = "myimage.png";
$image = imagecreatefrompng($file);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
header('Content-Type: image/jpeg');
$quality = 50;
imagejpeg($bg);
imagedestroy($bg);
?>

resizing an external image

Is there any script that resizes an external image and outputs it ?
For example, i want to resize all external square images to 130X130.
Like this
http://mydomain.com/script/resize.php?url=http://otherdomain.com/image.png
EDIT:
Facebook also is an example maybe
https://s-external.ak.fbcdn.net/safe_image.php?d=AQCYX3NIE5gMyujT&url=http%3A%2F%2Fi2.ytimg.com%2Fvi%2FyoLeJNjIVZk%2Fhqdefault.jpg
any help appreciated.
Thanks
// Content type
header('Content-Type: image/jpeg');
//get image from internet and save it into local disk
$url = 'http://www.google.com/images/srpr/logo3w.png';
$img = 'google.png';
file_put_contents($img, file_get_contents($url));
//get current size and set new size
list($width, $height) = getimagesize($img);
$new_width = 130;
$new_height = 130;
// genarate resized image copy
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefrompng($img);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// flush image to browser
imagejpeg($image_p, null, 100);
// save resised image to disk
imagejpeg($image_p, "newimage.jpg",100);
Use this code only if you want to resize and save on your application otherwise above example is enough.
Take a look at this thread. You can write your own code to resize images or use TimThumb script.
if you dont want to save them than you can just set the with and hight with html or css
<img height="130px" with="130px;" src="somesite.com/img.jpg"></img>

Black result image problem on BMP image resize using PHP

I have a PHP script to re size image file as below;
$file = "test.bmp";
$ext = pathinfo($file, PATHINFO_EXTENSION);
$info = pathinfo($file);
$file_name = basename($file,'.'.$info['extension']);
$thumbname = "thumb/".$file_name.".".$ext;
$maxh = 200;
$maxw = 200;
$quality = 100;
list($width,$height)=getimagesize($file);
$src = imagecreatefromwbmp($file);
$tmp = imagecreatetruecolor($maxw,$maxh);
imagecopyresampled($tmp,$src,0,0,0,0,200,200,$width,$height);
imagejpeg($tmp,$thumbname,$quality);
imagedestroy($tmp);
The script is suppose to resize a Windows bitmap image to 200x200 thumbnail. But instead, I am getting a black 200x200 image. I am using PHP with Apache in Windows PC. How can I fix this?
.bmp and wbmp are VERY, VERY different file types.
Note the content-type headers:
Content-Type: image/x-xbitmap
Content-Type: image/vnd.wap.wbmp
Calling imagecreatefromwbmp($file) where $file is a .bmp will fail every time.
See this thread for info on how to load a .bmp file. It's not pretty.
As pointed out in PHP imagecopyresampled() docs:
Note:
There is a problem due to palette image limitations (255+1 colors). Resampling or filtering an image commonly needs more colors than 255, a kind of approximation is used to calculate the new resampled pixel and its color. With a palette image we try to allocate a new color, if that failed, we choose the closest (in theory) computed color. This is not always the closest visual color. That may produce a weird result, like blank (or visually blank) images. To skip this problem, please use a truecolor image as a destination image, such as one created by imagecreatetruecolor().
To see if it's the case you can use imageistruecolor() and copy the contents to a new truecolor image before "copyresampling" it:
if( !imageistruecolor($src) ){
$newim = imagecreatetruecolor( $width, $height );
imagecopy( $newim, $src, 0, 0, 0, 0, $width, $height );
imagedestroy($src);
$src = $newim;
}
There is a new opensource project on Github that allows reading and saving of BMP files (and other file formats) in PHP.
The project is called PHP Image Magician.
<?php
//Create New 'Thumbnail' Image
$newImageWidth = 200;
$newImageHeight = 200;
$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
$newImageFile = 'output.jpg';
$newImageQuality = 100;
//Load old Image(bmp, jpg, gif, png, etc)
$oldImageFile = "test.jpg";
//Specific function
$oldImage = imagecreatefromjpeg($oldImageFile);
//Non-Specific function
//$oldImageContent = file_get_contents($oldImageFile);
//$oldImage = imagecreatefromstring($oldImageContent);
//Get old Image's details
$oldImageWidth = imagesx($oldImage);
$oldImageHeight = imagesy($oldImage);
//Copy to new Image
imagecopyresampled($newImage, $oldImage, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $oldImageWidth, $oldImageHeight);
//Output to file
imagejpeg($newImage, $newImageFile, $newImageQuality);

Making a .ico file using gd and php

Does anyone know how to make a .ico file that will work in Intenet explorer?
I can't seem to get anywhere
here is my gd code
$im = imagecreatefromjpeg(FAVICONDIR.'normal/'.$filename );
list($width, $height) = getimagesize(FAVICONDIR.'normal/'.$filename); // get the width and height of the jpg
$image_p = imagecreatetruecolor("16", "16"); // create a 16x16 canvas to play with
imagecopyresampled($image_p, $im, 0, 0, 0, 0, "16", "16", $width, $height); // resize jpg to 16x16
imagepng($image_p,FAVICONDIR.'icons/'.$ico_filename); // make a .png file (icon file) from our data
imagedestroy ($im); // close gd library
and an attenpt with image magick
/*$cmd = IMAGEMAGIKDIR .''. FAVICONDIR.'normal/'.$filename . ' -transparent white -background white -flatten -resize 16x16 ico:'.FAVICONDIR.'icons/'.$ico_filename;
exec($cmd);*/
Any ideas would be great.
Use png2ico if you run into problems this is a great tutorial
http://myutil.com/2007/10/14/favicon-ico-gimp
Richard

Categories