Resize and Convert all uploading image into png format - php

First I want to tell you guys I'm a beginner to this. I'm developing a website, I want to allow users to upload any size of images in a variety of formats (GIF,JPEG,PNG).
And I want to re size the image and convert the image format into PNG. Im not inserting the image into database as BLOBs.
this is my code, But I'm uploading the image directly here, How to re size and convert to PNG:
$myImage = $_FILES['imgCover']['name'];
$response = mysql_query("INSERT INTO gallery (imagename) VALUE ('$myImage')");
if($response === true)
{
move_uploaded_file($_FILES['imgCover']['tmp_name'],"images/gallery/".$_FILES['imgCover']['name'])
}
Please help me.

Something like http://salman-w.blogspot.com/2008/10/resize-images-using-phpgd-library.html?
Just use png where its says jpeg, like
imagejpeg
etc.

Related

how to convert any image file into EPS format in PHP?

I have an image processing system in PHP with Imagemagick. The existing system will be processing images of EPS format for some process and PNGs for the remaining process. So, I need to upload the same image file in EPS and PNG. I am doing the file upload facility now, which should automate the procedure of converting any format image file into EPS and PNG and should save in corresponding locations.
What I need now is to be able to convert any image format file into EPS and PNG, so then I can process and save them, but there are some DPI limitations. So I need to save the files into these EPS and PNG formats so that only the existing system can use those files properly.
Please advice me if there is any way to convert image files into EPS and PNG with PHP and Imagemagick.
Thanks in advance.
You can convert any image to eps format by following code
public function convertImageToEps(){
$imgUrl = WWW_ROOT.'imfcs.jpg';
$imagic = new Imagick();
$imagic->readImage($imgUrl);
$imagic->setImageFormat('eps');
$imagic->writeImage(WWW_ROOT.'simfcs.eps');
return true ;
}

PDF 2 Image using PHP without loss of quality?

I'm trying to convert a pdf to an image file (png, jpg, gif does not matter).
But using the following code:
<?php
$im = new imagick('helloworld.pdf[0]');
$im->setImageFormat('png');
header('Content-Type: image/png');
echo $im;
?>
Picture quality is degraded significantly.
Is there any way to convert it with no or very little quality loss ?
you have to play with Imagick::setCompression, Imagick::setCompressionQuality and Imagick::setImageCompression
for this guy helped
$im->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$im->setImageCompressionQuality(0);
anyway, you have to put some of this strings before you format the image to png

php iphone/IOS6 upload rotation issue: what is best way to save rotated image

Using the safari mobile browser with IOS6, the file upload function gives users the option to snap a photo. Unfortunately, upon snapping the photo, while the photo thumb shows up properly in the browser, when you upload to a server, the file is rotated 90 degrees. This appears to be due to the exif data that the iphone sets. I have code that fixes the orientation by rotating the image when serving. However, I suspect it would be better to save the rotated, properly oriented, image so I no longer have to worry about orientation. Many of my other photos do not even have exif data and i don't want to mess with it if I can avoid it.
Can anyone suggest code to save the image so it is properly oriented?
Here is the code that rotates the image. The following code will display the properly oriented image, however, what I want to do is save it so I can then serve it whenever I want without worrying about orientation.
Also I would like to replace impagejpeg call in code below so that any code works for gifs as well as jpgs.
Thanks for suggestions/code!
PHP
//Here is sample image after uploaded to server and moved to a directory
$target = "pics/779_pic.jpg";
$source = imagecreatefromstring(file_get_contents($target));
$exif = exif_read_data($target);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($source,90,0);
//echo 'It is 8';
break;
case 3:
$image = imagerotate($source,180,0);
//echo 'It is 3';
break;
case 6:
$image = imagerotate($source,-90,0);
//echo 'It is 6';
break;
}
}
// $image now contains a resource with the image oriented correctly
//This is where I want to save resource properly oriented instead of display.
header('Content-type: image/jpg');
imagejpeg($image);
?>
Only JPEG or TIFF files can carry EXIF metadata, so there's no need to worry about handling GIFs (or PNGs, for that matter) with your code.
From page 9 of what I believe is the official specification:
Compressed files are recorded as JPEG (ISO/IEC 10918-1) with application marker segments (APP1 and APP2) inserted. Uncompressed files are recorded in TIFF Rev. 6.0 format.
http://www.cipa.jp/english/hyoujunka/kikaku/pdf/DC-008-2010_E.pdf
To save your image just use the same function imagejpeg and the next parameter to save the image, something like:
imagejpeg($image, $target, 100);
In this case you don't need the specify the header, because you are not showing nothing.
Reference:
http://sg3.php.net/manual/en/function.imagejpeg.php

PHP Image Convert

In my web app users are allowed to upload images as their photos. How can I convert different image extensions to JPG? Input files are JPG, PNG or GIF.
Personally, I prefer Image Magick over GD. It's a lot better if you're dealing with large images too; you can run into memory allocation issues with GD.
With php, you can convert any image to an other using imagepng, imagejpeg, imagegif :
imagepng(imagecreatefromstring(file_get_contents($input)), 'output.png');
In this example, it will save the uploaded image in png with the path 'output.png'
You can use PHP GD.
For anybody who would want to get the binary out of a temporary file, here is my solution:
<?php
$temp = tmpfile();
imagepng(imagecreatefromstring($imgBinary), $temp);
$pathFile = stream_get_meta_data($temp)['uri']; // eg: /tmp/phpFx0513a
$pngBin = file_get_contents($pathFile)
?>

Saving a transparent image from a URL in PHP

I am trying to build a class that does many photo operations, one method will upload images from a user but I am also needing to build a method to grab a photo from a URL and run other methods on it just like if it were being uploaded with a POST form from user.
Below is my start of the function for getting image from URL, it works but needs work still. Below the code you can see a image that is the result of this function being ran. Also is the original image to see what it should look like. You can see that this function makes the image have a black background on this transparent image. How can I make it look better like it should look?
$url = 'http://a0.twimg.com/a/1262802780/images/twitter_logo_header.png';
//run our function
savePhotofromURL($url, 'no');
// photo function should grab an photo from a URL
function savePhotofromURL($photo_url, $saveimage = 'yes'){
if(isset($photo_url) && $photo_url != '') {
//get info about photo
$photo_info = getimagesize($photo_url);
$source_width = $photo_info['0'];
$source_height = $photo_info['1'];
$source_type = $photo_info['mime'];
//grab the Photo from URL
$photo = imagecreatefromstring(file_get_contents($photo_url));
if (is_resource($photo) === true){
if($saveimage === 'yes'){
// TO DO: resize image and make the thumbs code would go here if we are saving image:
// TO DO: resize source image if it is wider then 800 pixels
// TO DO: make 1 thumbnail that is 150 pixels wide
}else{
// We are not saving the image show it in the user's browser
// TO DO: we will add in correct photo type soon
header('Content-Type: image/gif');
imagejpeg($photo, null, 100);
imagedestroy($photo);
}
}else{
// not a valid resource, show error
echo 'error getting URL photo from ' .$photo_url;
}
}else{
// url of image was empty
echo 'The URL was not passed into our function';
}
}
The result looks like this
alt text http://img2.pict.com/52/05/1f/2429493/0/screenshot2b181.png
Instead of like this
The following two calls will tell php to use the alpha blending present in the png image:
ImageAlphaBlending($photo, false);
ImageSaveAlpha($photo, true);
Edit:
I see you're outputting the image as a JPEG also. JPEGs don't support transparency, so no matter what you do you will end up with an incorrect background color. Also see this related question: PHP/GD ImageSaveAlpha and ImageAlphaBlending
You need to add better support for image types and by extension their transparency.
Since the image is transparent we can know that its either a GIF or a PNG yet your sending the GIF header while using imagejpeg() - jpegs dont support any kind of transparency. But if its a png you may also have to account for if its alpha trans or index transparency.

Categories