I'm creating a png and uploading immediately to S3, I'd like to log how big that file was without having to do a seperate call to the same file on S3 to work out the size on disk.
Is this possible?
$tile = imagecreatetruecolor($tileImageSize, $tileImageSize);
imagecopy($tile, $resizedMainImage, 0, 0, $currentCoordsX, $currentCoordsY, $tileSize, $tileSize);
$writeStream = fopen("s3://bucket/file.png", 'w');
imagepng($tile, $writeStream, 9); // need filesize of this action
I've tried wrapping the imagepng in ob_start(); ob_get_length(); etc with no joy.
The thing is that you will need to save the image somewhere in order to get the size. To avoid writing to the filesystem, you can use the memory storage of php. I mean something like this:
imagepng($tile, 'php://memory/image.png');
$file_size = filesize('php://memory/image.png');
You can do this before uploading the image to the server to have the information for your log.
Related
This is the code I have:
$from = ...; // original uploaded file
$target = ...; // save path for optimized file
#unlink($target); // removes file if already exists
$image_o = imagecreatefromjpeg($from); // get a file
imagejpeg($image_o, $target, 85); // save optimized
imagedestroy($image_o); // free memory
But it doesn't do anything. This is going in loop, but now I test it on the single file. Seems to be working on smaller files. I tried image with size ~120 kB and optimized OK. But file with more than 1 MB, it will do nothing and those are files I really need to optimize. I also tried this:
set_time_limit(6000);
ini_set('memory_limit', '200M');
But no help. I'm working on a shared hosting, so I have no access to the php.ini or to install 3rd party PHP extension. Is there an alternative for imagecreatefromjpeg which seems to be the problem?
I've been searching the web and trying to understand how to scale and compress my image uploads with PHP. I want users to be able to upload, say, a 1MB file, but then to actually save a much more compressed version of that file to my server since for this application, details aren't as important. I've come up with the following code:
print_r($_FILES);
// Check if the file size is too big
if ($_FILES['image']['size'] > MAX_FILE_SIZE)
{
// Compress it
imagejpeg($_FILES['image']['tmp_name'], $_FILES['image']['tmp_name'], 60);
print_r($_FILES);
// Check file size again
if ($_FILES['image']['size'] > MAX_FILE_SIZE)
{
// Image too big still...
return;
}
}
At this point, I'm every time getting caught in my "// Check again" block and in both my "print_r" statements, I'm seeing the file size remain the same. Can anyone please point me in the right direction in terms of what I'm doing wrong? Is there an entirely different but better way of handling this? Thanks a lot!
Your imagejpeg function isn't being passed the correct arguments. You need to first open the file using a GD function like imagecreatefromjpeg and use the resource it returns to manipulate the image (in this case, compress it).
Try something like:
// Create image resource from file (try a different function if not JPG)
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
// Check if successfully opened
if($im){
// Resize the resource and save it back to the temporary file name
imagejpeg($im, $_FILES['image']['tmp_name'], 60);
}
I'm trying to resize image and upload it to FTP server without saving it on local disc but I didn't find any method how to do that. Function ftp_fput obviously doesn't TAKE image resource as argument.
I'm trying to do this:
imagecopyresampled($canvas, $image, 0, 0, 0, 0, $width, $height, $oldWidth, $oldHeight);
ob_start();
imagejpeg($canvas);
$stream = ob_get_clean();
// ftp_->fput(imagecreatefromstring($stream));
ftp_->fput($stream);
Any help?
PHP doesn't have a function to write a string through ftp. You would have to write the file to disk and send it or you can use the php://memory or php://temp wrapper to write the data to memory and pass that to ftp_fput. There is a comment on the ftp_put manual page that has a simple function that flushes to a temp file.
http://www.php.net/manual/en/function.ftp-put.php#83260
There are a few comments on the ftp_fput manual page that cover this as well.
I am trying to take a pic upload from a mobile device to a server. We are building with PhoneGap (Javascript), so we are having turn it into a string in order to send it to the server. I am having problems once I receive it, to turn it back into a readable image file.
Simply put, I need to take a string and a file name sent to me, decode it, convert it into a .png, then crop it into a circular image.
This is what I have going on currently
if (isset($_POST['file']))
{
//Result variable
$result = false;
$pic = base64_decode($_POST['file']);
$filename = $_POST['filename'];
if (strlen($pic) > 9 )
{
$fh = fopen("/var/www/pics/events/".$filename, 'w') or die("can't open file");
fwrite($fh, $pic);
fclose($fh);
}
}
I think I can get the rest of the code to work if I can figure out what I am doing wrong here that makes it not save properly as a image file? The file uploads correctly, but it stores with out an extension, and when I point to it in my browser, it comes up like it is supposed to be an image file, but never displays the image. That little broken picture icon with the colored shapes is what I get when I direct to it's location.
Do I need to be aware of what image type is being sent during this process at all? How is it knowing if it is a .gif, .jpg/jpeg, .png, etc...?
Thanks in advance for any help!
Nathan
For Security reasons you should sanitize the file name to prevent directory traversal.
On a brighter note, make sure the file is saved with the proper extension; if you are already saving with the correct extension you could have an encoding issue from the app.
If neither of the previous possibilities are the case make sure that your String Size does not exceed the maximum POST size limit in your php.ini; if that is the case increase the size limit.
I have moved my images to Rackspace Cloud Files and am using their PHP API. I am trying to do the following:
Get an image from my "originals" container
Resize it, sharpen it, etc.
Save the resized image to the "thumbs" container
My problem is with #2. I was hoping to resize without having to copy the original to my server first (since the images are large and I'd like to resize dynamically), but can't figure out how. This is what I have so far (not much):
$container = $conn->get_container("originals");
$obj = $container->get_object("example.jpg");
$img = $obj->read();
Part of the problem is I don't fully understand what is being returned by the read() function. I know $img contains the object's "data" (which I was able to print out as gibberish), but it is neither a file nor a url nor an image resource, so I don't know how to deal with it. Is it possible to convert $img into an image resource somehow? I tried imagecreatefromjpeg($img) but that didn't work.
Thanks!
First, you cannot resize an image without loading it into memory. Unless the remote server offers some "resize my image for me, here are the parameters" API, you have to load the image in your script to manipulate it. So you'll have to copy the file from the CloudFiles container to your server, manipulate it, then send it back into storage.
The data you receive from $obj->read() is the image data. That is the file. It doesn't have a file name and it's not saved on the hard disk, but it is the entire file. To load this into gd to manipulate it, you can use imagecreatefromstring. That's analogous to using, for example, imagecreatefrompng, only that imagecreatefrompng wants to read a file from the file system by itself, while imagecreatefromstring just accepts the data that you have already loaded into memory.
You can try to dump the content of the $img variable into a writable file as per the below:
<?php
$filename = 'modifiedImage.jpg';
/*
* 'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate
* the file to zero length. If the file does not exist, attempt to create it.
*/
$handle = fopen($filename, 'w+');
// Write $img to the opened\created file.
if (fwrite($handle, $img) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote to file ($filename)";
fclose($handle);
?>
More details:
http://www.php.net/manual/en/function.fopen.php
http://www.php.net/manual/en/function.fwrite.php
Edit:
You might also want to double check the type of data returned by the read() function, because if the data is not a jpg image, if it's for example a png, the extension of the file needs to be changed accordingly.