why imagepng after resize create a black image? - php

i m trying to resize a image using code:
list($width,$height,$type,$attr)= getimagesize($_FILES['upload'.$num]['name']);
$source = imagecreatefrompng($_FILES['upload'.$num]['name']);
$thumb = imagecreatetruecolor(445,320);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
imagecopyresampled($thumb,$source,0,0,0,0,445,320,$width,$height);
imagepng($thumb,"../public/img/".$Nome,8);
but the output is always a black image.. anyone know why?
Thanks

$_FILES['upload'.$num]['name'] is just filename of uploaded like "flower.jpg" not full path to file.
$_FILES['upload'.$num]['tmp_name'] is real absolute path to real file uploaded on your server (somewhere in temp directory)
Your code should look like this:
list($width,$height,$type,$attr)= getimagesize($_FILES['upload'.$num]['tmp_name']);
$source = imagecreatefrompng($_FILES['upload'.$num]['tmp_name']);
Always try to debug your first. Use functions like print_r($_FILES), var_dump($_FILES) to debug your variables.
except issue with $_FILES variable, your code should works fine: Demo

Related

how to get image file from absolute path in php

I have absolute path of my uploaded image file and i want to get image file which i uploaded in my folder. So how can i get image file and how i can upload image.So i can perform image operation like watermark and rotate. So i have to get image file in code igniter.
i tried but it is not working
$upld_file = 'my absolute path';
$real_path = realpath($upld_file);
$img = base64_encode($real_path);
and i send $img parameter to upload image
I am getting "you did not select a file to upload"
C:\wamp64\www\codeigniter_project\assets/images/1551892667300.jpeg
This is my absolute path of my image
Thank You.
You don't need to get the absolute path of the image if you are storing the images inside a assets in the root of your project directory, You can access the image like this
echo base_url('assets/images/1551892667300.jpeg');
NOTE : you need to load the url helper first
You can try like this..
//define image path
$filename="C:/wamp64/www/codeigniter_project/assets/images/1551892667300.jpeg";
// Load the image
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, 90, 0);
//and save it on your server...
imagejpeg($rotate, "savedestinationpath.jpg");
That should be the file input name. eg: If your file browse input name is myFileInput then it should be $this->upload->do_upload('myFileInput')
Also make sure that you have set attribute enctype="multipart/form-data" for your form.
One nice example is here

How to move image to a folder after applying ImageJPEG for compression?

I need some help regarding image compression & moving the compressed image to destination folder. i'm using:
ImageJPEG(ImageCreateFromString(file_get_contents('source file','target file','90')));
How can I upload the compressed file resulting from the above statement?
My Code:
if(!empty($_FILES['pic']['name'])){
$tempFile = $_FILES['pic']['tmp_name'];
$fileName = substr(sha1(rand(000,9999)),0,7).$_FILES['pic']['name'];
$targetPath = getcwd().'/assets/admin/images/image-gallery/';
ImageJPEG (ImageCreateFromString (file_get_contents ($tempFile,$fileName,90)));
$CompressedFile = $targetPath.$fileName;
copy($fileName,$CompressedFile);
Help me out with this please....
The second parameter in imagejpeg is the path to the output file, otherwise the image is just dumped to the screen.
PHP.net imagejpeg

Cannot copy and resample an image with PHP

I am trying to copy a resampled image. Something seems not working:
copy($_FILES['image']['name'],'logo.png');
$dir_subida = 'C:\xampp5\htdocs\bild';
$source = imagecreatefrompng($_FILES['image']['name']);
$destination = imagecreatefrompng($dir_subida . '\\logo.png');
imagecopyresampled($destination,$source,0,0,0,0,110,55,600,220);
First I copy the image and save it in a directory. Then, I define the source (an image that I am uploading in a form). Then I define the destination.
Then I try to resample and copy it with the default function imagecopyresampled.
It just copies the image right away without any kind of changes.
Am I missing something?
You must save the destination image to a file after you manipulate it.
imagepng($destination, 'c:\\path\\to\\file.png');

How to get and save THIS transparent image with PHP

I am trying to get a image from a URL, and then save it to the server. It is a simple task, yeah, but this image has transparency... The thing is, when i save that file to my PC or when I try to get and save it via PHP to my server, then I get the same result: the image gets all messed up with its transparency set to black. The URL of the image is:
http://cellufun.com/p/avatarimage/agb.aspx?i=Body|M04001|Shirt|M10020|
Another weird thing is my browser says the image is MIME Type image/png but I can't use imagecreatefrompng(); because I get the "image not a valid PNG" error...
Here is my code (I did try other solutions, this is what I tryed the last time):
<?php
$result=file_get_contents("http://cellufun.com/p/avatarimage/agb.aspx?i=Body|F04001|Shirt|F10020|Pants|MO55|");
$img = imageCreateFromstring($result);
imageAlphaBlending($img, true);
imageSaveAlpha($img, true);
imagecolortransparent($img);
header("Content-type: image/png");
imagepng($img);
?>
Oh, and I just tryed this image against the copy() function, and still get the same result... It looks like the image is smaller in size then the original image...
Just tryed this:
file_put_contents("files/test.png",file_get_contents($result));
and still not working... so it has something to do with the image itself, because whatever i try to get the data, it does not work.
Try this code (changes at lines 2-4):
<?php
$img = imageCreateFromgif("http://cellufun.com/p/avatarimage/agb.aspx?i=Body|M04001|Shirt|M10020|");
$index = imagecolorexact($img, 0, 0, 0);
imagecolortransparent($img, $index);
header("Content-type: image/png");
imagepng($img);
?>
It works for me although it's a little trick ;) ..and I don't know exactly why the quality is worse..
PS: The main problem I see is that the image at the url you provided is not really a png.

How to use imagick's writeImage() function?

This works if I keep the script in the same directory as the image being manipulated. And the resultant image "foo.jpg" is also generated in the same location.
<?php
$im = new imagick('image.jpg');
$im->thumbnailImage( 200, 0);
$im->writeImage("foo.jpg");
?>
But what if the script is in one location and the image I wish to work with is in another and the location I wish to save the thumbnail to is somewhere else, how to specify these paths?
Doing something like this doesn't work:
$im = new imagick('path/to/image.jpg');
Might be some file system problem. Try and get a file pointer in php first and check for any problems
$fileHandle = fopen("path/to/image.jpg", "w");
You can then use Imagick function (version 6.3.6 or newer);
$im->writeImageFile($filehandle);

Categories