i am trying to copy remote image file and reducing the size of image file and uploading to server but image file is created but it is not working .it gives error that image can not be displayed as it contains errors.
my code is this
<?php
$url = 'http://www.indiancinemagallery.com/gallery/vaani-kapoor/Vaani-Kapoor-at-Radio-Mirchi-Stills-(9)9678.jpg';
$img = '/home/xxxxxxx/public_html/xyyyyyy/test.jpg';
$im = imagecreatefromjpeg($url);
$fimg=imagejpeg($im, NULL, 60);
file_put_contents($img, file_get_contents($fimg));
?>
You need to read it from file by getting content, write it to file and read from file like;
<?php
$url = 'http://www.indiancinemagallery.com/gallery/vaani-kapoor/Vaani-Kapoor-at-Radio-Mirchi-Stills-(9)9678.jpg';
$img = '/home/xxxxxxx/public_html/xyyyyyy/test.jpg';
file_put_contents($img, file_get_contents($url));
$im = imagecreatefromjpeg($img);
$fimg=imagejpeg($im, NULL, 60);
?>
Why are you trying to save the image twice.
Your first try is incorrect, it should be
imagejpeg($im, $img, 60);
and don't do the file_put_contents
file_get_contents take a string as the parameter. imagejpeg just output image to browser or file, and return a boolean.
$url = 'http://www.indiancinemagallery.com/gallery/vaani-kapoor/Vaani-Kapoor-at-Radio-Mirchi-Stills-(9)9678.jpg';
$img = '/home/xxxxxxx/public_html/xyyyyyy/test.jpg';
file_put_contents($img, file_get_contents($url));
Related
It's a simple code to resize an image and send it to ftp server:
$info = getimagesize($_FILES["personalPhoto"]["tmp_name"]);
$image = imagecreatefromjpeg($_FILES["personalPhoto"]["tmp_name"]);
ob_start();
imagejpeg($image,null, 1);
$resizedImage = ob_get_contents();
ob_end_clean();
ftp_put($ftpConn,'/Kamil/HostMe/AllImages/'.$fileName.'.jpg',$_FILES["personalPhoto"]["tmp_name"],FTP_BINARY);
ftp_put($ftpConn,'/Kamil/HostMe/AllImages/'.$fileName.'.jpg',$resizedImage,FTP_BINARY);
The first ftp_put command works fine (sends the original image to server)
the second ftp_put command which is supposed to send the resized image is not working. any ideas?
$resizedImage is a PHP variable, not a physical file. To solve your problem, you can write $resizedImage into a file then set this to ftp_put. Such as:
$file = "/tmp/somefile.jpg";
file_put_contents($file, $resizedImage);
ftp_put(
$ftpConn,
'/Kamil/HostMe/AllImages/'.$fileName.'.jpg',
$file,
FTP_BINARY
);
Want to take image from own server rotate certain angle and save the image.
Image file $filename = 'kitten_rotated.jpg'; With echo '<img src='.$filename.'>'; i see the image.
Then
$original = imagecreatefromjpeg($filename);
$angle = 90.0;
$rotated = imagerotate($original, $angle, 0);
Based on this https://stackoverflow.com/a/3693075/2118559 answer trying create image file
$output = 'google.com.jpg';
If i save the same image with new file name, all works
file_put_contents( $output, file_get_contents($filename) );
But if i try to save rotated image, then file_put_contents(): supplied resource is not a valid stream resource.
file_put_contents( $output, $rotated );
Here https://stackoverflow.com/a/12185462/2118559 read $export is going to be a GD image handle. It is NOT something you can simply dump out to a file and expect to get a JPG or PNG image.. but can not understand how to use the code in that answer.
How to create image file from $rotated?
Tried to experiment, based on this http://php.net/manual/en/function.imagecreatefromstring.php
$fh = fopen( 'some_name.png' , 'w') or die("can't open file");
fwrite($fh, $data );
fclose($fh);
Does it means that need something like
$data = base64_encode($rotated);
And then write in new file?
I have not tested this, but I think you need to encode the image as base 64 first.
If you check the string from any Image URL, you'd see data:image/png;base64, preceding the hash. Prepending this to your image string and saving.
Here is a function that may help, based on what you already have:
// Function settings:
// 1) Original file
// 2) Angle to rotate
// 3) Output destination (false will output to browser)
function RotateJpg($filename = '',$angle = 0,$savename = false)
{
// Your original file
$original = imagecreatefromjpeg($filename);
// Rotate
$rotated = imagerotate($original, $angle, 0);
// If you have no destination, save to browser
if($savename == false) {
header('Content-Type: image/jpeg');
imagejpeg($rotated);
}
else
// Save to a directory with a new filename
imagejpeg($rotated,$savename);
// Standard destroy command
imagedestroy($rotated);
}
// Base image
$filename = 'http://upload.wikimedia.org/wikipedia/commons/b/b4/JPEG_example_JPG_RIP_100.jpg';
// Destination, including document root (you may have a defined root to use)
$saveto = $_SERVER['DOCUMENT_ROOT']."/images/test.jpg";
// Apply function
RotateJpg($filename,90,$saveto);
If you want to save image just use one of GD library functions: imagepng() or imagepng().
imagerotate() returns image resource so this is not something like string.
In your case just save rotate image:
imagejpg($rotated, $output);
And now You can use $output variable as your new filename to include in view like before:
echo '<img src='.$output.'>';
Don't forget to include appropriate permissions in directory where You're saveing image.
I'm trying to load an image, edit it, save it and then display it from a script that is called within IMG tags. The script works if I want to just display the image and it does save the image. But it won't save it and then display it. Does anyone know what I'm doing wrong? Any help would be greatly appreciated.
<?php
header('Content-Type: image/png');
$file_location = "test.png";
if (file_exists($file_location)) {
$img_display = #imagecreatefrompng($file_location);
// This section of code removed as doesn't affect result
imagepng($img_display, $file_location);
chmod($file_location, 0777);
imagepng($img_display);
imagedestroy($img_display);
}
?>
Try this and checks if your folder has permission to save the image. chmod 777 on it for sure.
<?php
header('Content-Type: image/png');
$file_location = "test.png";
if (file_exists($file_location)) {
$img_display = imagecreatefrompng($file_location); // Create PNG image
$filename = $file_location + time(); // Change the original name
imagepng($img_display, $filename); // Saves it with another name
imagepng($filename); // Sends it to the browser
imagedestroy($img_display); // Destroy the first image
imagedestroy($filename); // Destroy the second image
}
Try this:
ob_start();
imagepng($img_display);
$contents = ob_get_clean();
file_put_contents($file_location, $contents);
echo $contents;
imagedestroy($img_display);
hi guys ive created a base64 encoded image captured with web cam now i convert the .png to .jpg all works fine but now i get two images on server both .png and .jpg how do i go about deleting the .png or is their a way to convert to jpg without saving .png image to disk thanx here my code
$rawData = $_POST['imgBase64'];
$filteredData = explode(',', $rawData);
$unencoded = base64_decode($filteredData[1]);
$randomName = rand(1000, 99999999999);
//Create the image
$fp = fopen('user/'.$randomName.'.png', 'w');
fwrite($fp, $unencoded);
//convert image from png to jpg
$image = imagecreatefrompng('user/'.$randomName.'.png');
imagejpeg($image, 'user/'.$randomName.'.jpg', 80);
unlink($fp);
ive tried it with
unlink($image);
unlink($_SERVER['DOCUMENT_ROOT'] . "/user/.$randomName.'.png'");
imagedestroy($fp);
imagedestroy($image);
Use the function unlink() but passing the file name to it instead of the file handler.
So from your example it would be:
EDIT: You might need to close the file first:
fclose( $fp );
unlink( 'user/'.$randomName.'.png' );
as far as i understand all you need is:
$data = base64_decode( $_POST['imgBase64']);
// image resource from your string
$image = imagecreatefromstring($data);
imagejpeg($image, 'user/'.$randomName.'.jpg', 80);
So what I'm trying to do is:
- given an image url -> convert image to png
- zip resulting png
I have the following code which successfully does the conversion and zipping (I'm going to expand it later to test the extension to auto convert formats):
$file = "../assets/test.jpg";
$img = imagecreatefromjpeg($file);
imagePng($img, "files/temp.png" );
$zip->addFile( "files/temp.png", "test.png" );
What I want to know is, is it possible to do this without creating a copy of image before it's zipped
See ZipArchive::addFromString().
$file = "../assets/test.jpg";
// capture output into the internal buffer
ob_start();
$img = imagecreatefromjpeg($file);
imagepng($img);
// get contents from the buffer
$contents = ob_get_clean();
$zip = new ZipArchive();
$zip->open('archive.zip', ZipArchive::CREATE);
// and put them in the zip file...
$zip->addFromString('name_in_the_zip.png', $contents);