How to upload image file in alfresco using PHP - php

I want to upload image file into alfresco using cmis api using PHP..
I can create simple text document in alfresco using following code
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, "testssss", "text/plain");
I tried following code to upload image
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, null, "image/jpeg");
But can't create image file into alfresco
Can anyone help me to solve this issue ??

I got the solution of this problem
Just store base64 content into image.. Use below code
$filename="A.jpg";
$handle = fopen($filename, "r");
if(!$handle)return FALSE;
$contents = fread($handle, filesize($filename));
if(!$mimetype)$type=mime_content_type($filename);
else $type=$mimetype;
fclose($handle);
$base64_content=base64_encode($contents);
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, base64_decode($base64_content), "image/jpg");

Related

Set an extension and make full file from base64 encode to able to use laravel file method in php

I had base64 encoded data.
Look my code, please.
First of all, see my code.
$data = "data:image/png;base64,iVBORw0KGgoAAAA..........";
$image_array_1 = explode(";", $data);
$image_array_2 = explode(",", $image_array_1[1]);
$data = base64_decode($image_array_2[1]);
$imageName = uniqid().time().".png";
I want to set an extension .png to complete my file so that I can count this file extension by laravel method $image->getClientOriginalExtension() and others laravel file methods.
sorry for miss spell of language.
Hope I make you understand.
This works, although I cannot say if it's the best way to go about it. It's a full working example with a 1x1 black pixel png image. This assumes you already removed the data:image/png;base64, portion from the image data.
$data = base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR
42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=');
// Create a temp file and write the decoded image.
$temp = tmpfile();
fwrite($temp, $data);
// Get the path of the temp file.
$tempPath = stream_get_meta_data($temp)['uri'];
// Initialize the UploadedFile.
$imageName = uniqid().time().".png";
$file = new \Illuminate\Http\UploadedFile($tempPath, $imageName, null, null, true);
// Test if the UploadedFile works normally.
echo $file->getClientOriginalExtension(); // Shows 'png'
$file->storeAs('images', 'test.png'); // Creates image in '\storage\app\images'.
// Delete the temp file.
fclose($temp);

How to save images from list image url in csv

I want to save images from url to my directory and its image name after downloaded into a csv.
I write the following code which store images. but its not working as I want.
in below code $img is variable which fetch url from csv.
suppose I have 1000+ products in csv and every products have image url like
www.example.com/images/image1.jpg
so I want to download this image to my local directory/server directory and store its image name after download to csv file so I can add only image name into database tables.
set_time_limit(1000);
while($url = $response->fetch()) {
$my_image = file_get_contents($img);
$my_file = fopen('/csvfiles/','w+');
fwrite($my_file,$my_image);
fclose($my_file);
}
The some solution I found on other similar questions is
$file = file_get_contents($img);
file_put_contents("/csvfiles/Tmpfile.zip", fopen($img, 'r'));
But it is not applicable in my condition or not working.
Any help would be appreciated.
Edit New -
As marked as duplicate I want to add some more info here -
As I have all products info in csv formats and while importing the products I also want to download images from its url. And it is not possible to download 1000+ product image manually. I checked the above solution but it is not applicable in this situation. As in above solution they download image from only particular page and rename it directly. so it is different scenario here.
New Code - I also tried this code still not working
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
$fp = fopen("/csvfiles/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
Try with the following code.
set_time_limit(1000);
while ($url = $response->fetch()) {
$my_image = file_get_contents($img);
// check stricly that you have content of image
if ($my_image !== false) {
// download the image and store in the database.
$my_file = fopen('/csvfiles/', 'w+');
fwrite($my_file, $my_image);
fclose($my_file);
}
}
You need a filename as well, not only a directory:
// download the inage from the internet:
$my_image = file_get_contents($url);
// get filename from url:
$filename = basename($url);
$path = '/csvfiles/' . $filename;
// save image to that file:
$my_file = fopen($path,'w+');
fwrite($my_file, $my_image);
fclose($my_file);
// save $filename to DB
Hi below code works for me .. hope it will help someone..
Thanks #Gavriel and #Alankar
if(!empty($img)){
$my_image = file_get_contents($img);
file_get_contents(filename)
$filename = $data[0] . ".jpg";
echo $filename. "\n";
if(!file_exists($filename))
{
$my_file = fopen($filename,'w+');
file_put_contents($filename, $my_image);
echo $filename . "\n";
}
}
$image = "/uploads/" . $filename;
echo $image . "\n";
Here $img store url of image. which is fetch from csv file.
Here data[0] is csv column which is used to store image name uniquely . so we can use file_exists(); otherwise whenever I run my script again and again it store images again and store it by random temp name.

Image becomes empty when uploading from JSON using POSTMAN

I am trying to upload an image from JSON code.
But when it is uploaded its data is stored in the folder. But data is lost(the file exist but has 0 bytes) I don't know if the problem is exactly from where. If anyone know please help me.
This is the code
public function decodeImage($imageData)
{
$imageData = $dat->imageData;
$this->decodeImage($imageData);
$filename = "sampletest";
$binary=base64_decode($imageData);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('./'.$filename, 'wb');
fwrite($file, $binary);
fclose($file);
}

Download a thubnails of an image using Dropbox API for php

I made a script where I download the images from a dropbox folder to my computer using PHP.
What I try to do now is to download thumbnail of the images instead of the whole image.
For this I use the: GetThumbNail method from the Dropbox API.
Here is part of the code:
// download the files
$f = fopen($img_name, "w+b");
$fileMetadata = $dbxClient->getThumbnail($path, 'jpeg','xl');
fclose($f);
When I run this the images I get are 0 size and they have no content. Any ideas what I am missing?
Thanks
D.
EDITED
$f = fopen($img_name, 'w+b');
$thumbnailData = $dbxClient->getThumbnail($path, 'jpeg', 'xl');
fwrite($f, $thumbnailData);
fclose($f);
You're opening and closing $f without ever writing anything into it.
getThumbnail returns an array with two elements: the metadata for the file and the thumbnail data.
So I think you'll want something like this:
$f = fopen($img_name, 'w+b');
list($fileMetadata, $thumbnailData) = $dbxClient->getThumbnail($path, 'jpeg', 'xl');
fwrite($f, $thumbnailData);
fclose($f);

Upload .xls file on PHP server using the HTML5 File API

I all,
I'm trying to upload an xls file using the HTML5 API.
I already have a script which is working great for images.
It's also working with an xls file except the fact that I can't open the file using excel once it has been uploaded...
If I compare the size, the orignal file is 251 904 octets while the upload one is 251 911 octets. I don't know if that matter.
Here is what i've done :
Javascript:
reader = new FileReader();
reader.onloadend = function(evt) {
that.file = evt.target.result;
};
reader.readAsDataURL(file); // I've also try with readAsText but it's worse
the file is send using Sproutcore Framework:
SC.Request.postUrl(url).json()
.notify(this, 'fileUploadDidComplete', fileName)
.send({ fileName: fileName, file: file });
PHP:
$httpContent = fopen('php://input', 'r');
$json = stream_get_contents($httpContent);
fclose($httpContent);
$data = json_decode($json, true);
$file = base64_decode($data['file']);
$fileName = substr(md5(time().rand()), 0, 10).$data['fileName'];
$filePath = GX_PATH."/files/tmp/".$fileName;
$handle = fopen($filePath, "w");
fwrite ($handle, $file);
fclose($handle);
I hope somebody will help me to find what is wrong.
Thanks in advance.
EDIT:
I find another way which work on more browsers. I've discover FormData !
formData = new FormData();
formData.append("file", file);
SC.Request.postUrl(url).notify(this, 'formDataDidPost').send(formData);
This way, the PHP code is more simple :
$_FILES['file'];
I have done this using following method
$out = fopen($fileUploadPath ,"wb");
$in = fopen("php://input", "rb");
stream_copy_to_stream($in,$out);
This is just 3 methods I used to write the uploaded content to the server. Try it and see

Categories