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
}
?>
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);
Since in wordpress, the files/images uploaded are stored in 3 different sizes thus taking up the memory. I have a code that re sizes the image given the URL of that image. The code to resize is:
$img = wp_get_image_editor( $image_url );
if ( ! is_wp_error( $img ) ) {
$img->resize( 200, 200, false );
$filename = $img->generate_filename(
'final',
ABSPATH.'wp-content/uploads',
NULL
);
$img->save($filename);
}
So I want to use this code to resize the image from the local path of the user so that I don't use up too much of my memory.
Can anyone tell me how to get the local path and url of the file uploaded by url?
How about this for getting the local path of an image from its URL? :
function ContentUrlToLocalPath($url){
preg_match('/.*(\/wp\-content\/uploads\/\d+\/\d+\/.*)/', $url, $mat);
if(count($mat) > 0) return ABSPATH . $mat[1];
return '';
}
It assumes the file is located in the uploads folder.
we could use get_attached_file() for retrieve attached file path based on attachment ID
try:
<?php
get_attached_file( $attachment_id);
?>
I am saving canvas to a file. Code creates a png file in the upload folder. It is working correctly on local machine but when i try to run this on the server, I am not able to find the file in the upload folder. am i giving wrong path ?
After file creation i am printing alert, so i get file creation alert but file is not just created in the upload folder .
if ( isset($_POST["image"]) && !empty($_POST["image"]) ) {
// get the image data
$data = $_POST['image'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
//Image name
$filename ="image". md5(uniqid()) . '.png';
$file ='../upload/'.$filename;
// decode the image data and save it to file
file_put_contents($file,$data);
}
make sure directory '../upload' exists or create it first