how to pass file name to ftp_put function? - php

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
);

Related

php take image, rotate and save rotated image on server

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.

unlink base64 encoded image not working

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);

file_get_contens from remote url not working

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));

Image Remote Upload Script

I'm having trouble to make a php file that'll be able to copy remote images and save it to a directory.
so i'm going to input a set of image urls into the first text box, and when run the image will grab those images and save them to a specified directory and display the new set of urls. :)
How can i do that?
Place this code in your script.php
<?php
$image = 'http://remote.com/image.jpeg';
file_put_contents(dirname(__FILE__).'/'.basename($image), file_get_contents('http://remote.com/image.jpeg'));
This should start you off
header("Content-Type: image/jpeg");
echo file_get_contents("http://www.somewebsite.com/images/image.jpg");
You could then save this file to a local directory
I'd start by using file_get_contents() along with the fopen(), fwrite(), and fclose() gang.
<?php
foreach( $_POST['image'] as $image ) {
$f = fopen( "/path/to/image/folder/".basename( $image ) ); //Open image file
fwrite( $f, file_get_contents( $image ) ); //Write the contents of the web image to the newly created image on your server
fclose( $f ); //Close the file
}
?>

php upload image with ftp problem

I am using the code below to upload an image through ftp
$sFile=$ftp_dir."/".$image_name;
$image=$database_row["image"];//image is store in database
$fh = tmpfile();
$fwrite($fh, $image);
$uploadFile = ftp_fput($conn_id, $sFile, $fh, FTP_ASCII);
fclose($fh);
The ftp is creating the file and has a size BUT the file i get is not an image.When try to open on image viewer i get error.
Before switch to ftp i had this code
$image=$database_row["image"];//image is store in database
$file = fopen( "images/".$image_name, "w" );
fwrite( $file, $image);
fclose( $file );
and was working fine, but now i have to use ftp.
What am i missing.
You need to fseek to the beginning of the file after writing content to it and you need to use binary upload mode:
$sFile=$ftp_dir."/".$image_name;
$image=$database_row["image"];//image is store in database
$fwrite($fh, $image);
fseek($fh, 0);
$uploadFile = ftp_fput($conn_id, $sFile, $fh, FTP_BINARY);
fclose($fh);
Try using FTP_BINARY instead of FTP_ASCII. If all else fails, open the resulting file with a hex editor.
you are telling ftp to read the image as ascii (text)
change it ot FTP_BINARY.
//turn passive mode on then it will work fine
ftp_pasv($conn_id, true);

Categories