I have a form in my Android app that send information to php server with an image pick button. I want to resize image before saving on server with php codes :
<?php
move_uploaded_file($_FILES['file']['tmp_name'],'uploads/'.$_FILES['file']
['name']);
$orgfile='uploads/'.$_FILES['file']['name'];
list($width,$height)=getimagesize($orgfile);
$newfile=imagecreatefromjpeg($orgfile);
$thumb='uploads/a/'.$_FILES['file']['name'];
$truecolor=imagecreatetruecolor(600,400);
imagecopyresampled($truecolor,$newfile,0,0,0,0,600,400,$width,$height);
imagejpeg($truecolor,$thumb,100);
unlink($orgfile);
?>
This code just resize jpeg images and another formats (png or gif and even jpg) saved a black image.
It is necessary to mention that name of image file changed to a random number like "32165465423" and I don't know the image format to use "imagecreatefrompng" or "imagecreatefromgif" in my php file.
I want a code like "imagecreatefromall" or another ...
Thanks guys(sorry for bad English)
You will have to detect the type of image, based on that you can run the function. See the one cool php library for reference
https://github.com/eventviva/php-image-resize/blob/master/lib/ImageResize.php#L77
Related
I have created images using PHP image GD library and have them stored on my server, For the purpose of keeping some images uncached I wrote php code to fetch some specific images and output the php page as image through php headers:
$image=imagecreatefrompng($image_location);
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
surprisingly, the image from php page is always slightly smaller in size than the original static file using which the image was created. Original PNG file was 11.5KB in one case, while the php png file of the same static file was 11.3KB
The original png image was created using
imagecreate(), imagecolorallocate(), imagettftext()
and
imagepng($image,$location,9,PNG_ALL_FILTERS)
Why is the original image itself always bigger than the original? How can I reduce the size first time itself? Is there something "un-optimized" about my code?
Please help me out, even a 10% saving on size will help me hugely.
Source image
http://i.imgur.com/TbffELG.jpg
This is a vertical image, but when I upload it to server, server got the size width="3264" height="1836", and my resize and crop function will be wrong
there is the demo site http://demo.chan15.info/im/
PHP code
<?php
$file = $_FILES['file'];
$tmp = $file['tmp_name'];
$imageInfo = getimagesize($tmp);
echo '<pre>'; var_dump($imageInfo); echo '</pre>';
If the photo was taken on a mobile phone it can apply meta data regarding the orientation of the device at the time, which is used to infer the correct way to display the image. Not all decoders support the meta data, and will display the image incorrectly. In this case your image will probably display on its side.
You either need an image library capable of dealing with this meta data, or you can transform the image and/or remove the meta data. Apologies, but I can't suggest a suitable image library.
I had the same Problem. As HenryTK said the central problem is that the orientation of the picture is stored in the EXIF data (used by modern cameras and smartphones) while the width and length information indicate a landscape picture.
My solution is to open the picture with GIMP. GIMP shows the following modal dialog:
Now you only have to click the rotate button and save the picture.
It's not the best solution. If somebody has a script or something please tell me.
I have a question about displaying images in PHP. I need to have a PHP file display as JUST an image, as opposed to an image embedded in a web page, as if you had browsed to the JPEG image directly. The reason that I need it to be a PHP page as opposed to actually browsing to the image is that I need to resize the image before it is delivered. It would be easiest to use an image directly because that way I can display the image in a desktop application more easily. Is there any way to do this? Thanks!
<?php
header('Content-Type: image/jpeg');
readfile('path/to/image.jpeg');
You need to set the header Content-Type property to be the correct image type. See this link for the some examples.
http://www.electrictoolbox.com/image-headers-php/
Choose the correct header :
header("Content-type: image/png");
And use php gd to change the image size : http://php.net/manual/fr/book.image.php
I have a small Minecraft server where people can upload their skins. Minecraft skins are small png images. Is it possible to convert this png image to another png image via PHP (e.g. GD library)?
I have made this image to help me explain what I am trying to do:
Yes, it's possible. You'd need multiple imagecopy commands to pull out sections of the skin image and paste it into the proper spots in the "output" image.
Basic order of operations would be:
$input = imagecreatefrompng('skin.png');
$output = imagecreatetruecolor(800, 600); // whatever the dimensions should be.
imagecopy($output, $input, 0,0, 10,20, 50,60);
imagecopy(...);
...
...
The first copy command is saying "take a 50x60 section of the input image, starting at coordinates 10x20, and paste it into the destination image in the top left corner".
The actual sequence/coordinates/sizes will be up to you to figure out.
If you're not doing a 1:1 copy of the image and are doing resizing, then you'll want imagecopyresampled() instead.
Here is the PHP manual for creating images from png :
http://php.net/manual/en/function.imagecreatefrompng.php
Here is a simple tutorial :
http://www.phptutorial.info/?imagecreatefrompng
You can do this with CSS
Here is a tutorial: http://www.w3schools.com/css/css_image_sprites.asp
I am working on building gallery where the user uploads all the images. I had tried to use GD originally but found that it used way too much memory when dealing with images from a digital camera. So I have been looking into ImageMagick and ran into this problem.
My end goal is to resize the image and then upload it. I am not sure if this is possible with ImageMagick or not. I have gotten it to resize the image after upload but it doesn't save the resized image, just the original size.
This is the code I am currently using: ($image is the path to the file on my server)
$resource = NewMagickWand();
MagickReadImage($resource,$image);
MagickSetImageCompressionQuality( $resource, 100);
$resource = MagickTransformImage($resource,'0x0','660x500');
Any input would be appreciated,
Levi
Your code will send the modified image to the client (the web browser), but it will not save it to the server (replacing the original image, for example)
To save the image, use:
MagickWriteImage( $resource, 'new_image.jpg' );