I'm using the cloudinary face detection API this way:
require 'cloudinary/Cloudinary.php';
require 'cloudinary/Uploader.php';
require 'cloudinary/Api.php';
\Cloudinary::config(array(
"cloud_name" => "xxxxxxxxxxxx",
"api_key" => "9999999999999",
"api_secret" => "xxxxxxxxxxxxx"
));
$img = 'guy.jpg';
$imgid = time() . 'guy';
\Cloudinary\Uploader::upload($img, array("public_id" => $imgid));
$url = 'http://res.cloudinary.com/xxxxxx/image/upload/c_fill,g_face,h_500,w_375/' . $imgid . '.jpg';
file_put_contents('cropped' . $img, file_get_contents($url));
But what I also need is:
\Cloudinary\Uploader::upload($img, array("public_id" => $imgid));
// what I need >>>
if('face_count != 1')
{
exit;
}
// <<< what I need
$url = 'http://res.cloudinary.com/xxxxxx/image/upload/c_fill,g_face,h_500,w_375/' . $imgid . '.jpg';
file_put_contents('cropped' . $img, file_get_contents($url));
I don't understand how to use the face_count from the doc here: http://cloudinary.com/documentation/image_transformations#specifying_conditions
Thanks for the answer Nadav, I solved it this way:
$url = 'http://res.cloudinary.com/xxxxxxx/image/upload/if_fc_eq_1,c_fill,g_face,h_500,w_375/' . $bildnamecloud . '.jpg';
file_put_contents('cropped' . $bildname, file_get_contents($url));
\Cloudinary\Uploader::destroy($bildnamecloud);
list($width, $height) = getimagesize('cropped' . $bildname);
if($width == 375 AND $height == 500)
{
}
else
{
}
Related
I am beginner in Laravel.
I have this code:
if ($request->hasfile('profilePhoto')) {
$this->validate($request, [
'profilePhoto' => 'required',
'profilePhoto.*' => 'mimetypes:image/jpg'
]);
$image = $request->file('profilePhoto');
$extension = strtolower($image->getClientOriginalExtension());
$path = 'upload/images/UserImage/';
$uniqueName = md5($image . time());
$image->move(public_path($path), $uniqueName . '.' . $extension);
}
This function uploads files to public/upload/images/UserImage/.
I need it to store it in storage/app/upload/images/UserImage/ instead
How can I rewrite my code?
You have to use storage_path function ("storage/app/upload" folder must exist):
$image->move(storage_path("app/upload"), $uniqueName . '.' . $extension);
if ($request->hasfile('profilePhoto')) {
$this->validate($request, [
'profilePhoto' => 'required',
'profilePhoto.*' => 'mimetypes:image/jpg'
]);
$image = $request->file('profilePhoto');
$extension = strtolower($image->getClientOriginalExtension());
$path = storage_path('app/public/upload/images/UserImage');
$uniqueName = md5($image . time());
$image->move(public_path($path), $uniqueName . '.' . $extension);
}
A common way in Laravel to upload to storage/app is through the local disk driver.
$file = $path . $uniqueName . '.' . $extension;
\Storage::disk('local')->put($file, $request->file('profilePhoto'));
Storage::disk('local') points to storage/app/.
As your $path variable is already declared like this $path = 'upload/images/UserImage/' .So instead of storing data to public_path you can store to storage_path().
$image->move(storage_path($path), $uniqueName . '.' . $extension);
I am trying to upload the binary image to the storage using Laravel Intervention Image but it gives me error as Unable to init from given binary data.
I am using this code
$image = base64_decode($postData['image']);
$destinationPath = storage_path($destinationFolder);
if (!File::exists($destinationPath)) {
File::makeDirectory($destinationPath, 0777, true, true);
}
$filename = ($fileName != '') ? $fileName : $folderName . '_' . time() . '.jpg';
$imageResult = Image::make($image)->resize($imageWidth, $imageHeight, function ($constraint) {
$constraint->aspectRatio();
})->save($destinationPath . $filename, imageQuality($image));
if ($imageResult){
return '/image/' . $filename;
}
return false;
The binary image data is
data:image/webp;base64,UklGRlIFAABXRUJQVlA4IEYFAAAQHACdASpPAHkAPlEQlEojkdHMYDgFBLIAZqA25eJVGRUWLtSnk7iTzHecd0SXUx8+t+zLWt8WJpEWrTTvLe9y9CX9VSAw3YOVEdn4oGm0ZIrnQUIJ7VsI/r+aW0VOJeFoJylth8MmFQHlbPEklNUVbgyVJnINmgXnDbtgj9paOvkDYnVAdv2ErJONHxXyp2eyn7mB6vcVu9AfWuUtDlYxhQG1CEongtTYR0U4jQIbXYTTzN/3G5cAEd0FmVN272q9XHtEAeWzrMHHsZ7YPtpmppalPe8BvhnrwSA+ctaG9iQ6b7pEVBVPAAD+/uy97OjEKfy53WiZp+vshoaLbmP0cVKb4k6hnKsxJdcP+CgaRYQkOjb4FGLJO55Q/c+afu5UzMOW3Tx4pq6YezZ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xvWT1UhTg7J/9J/4cCoBkweCOc1exDC4bBpdmwNyB82TfMOL+HcsO/UR1EDkEBdEl2HLQOA2mSntOQ3dTgMQX/71+MtpetptbThjL/fnRMMfVnpovAe2jd4SXx+s8fMNJXcEkO9ZKxIWFH0EwLvzIp8SPc5z+AzLEwfn4aBvVL835u4BsodI+usLpWnlC7Xdz6JMVxKivRpoWgrcIsAF91564sRuvXd3rwozLc0Fb/at7P3B3B5Jc7wPXQ/CH5Y6Thjn0W8zAWnd3VwwB3ZS/Uv/ubq8H7W9He6fvg6Ib/101yfwV8xpmLHwZGmXWgAAAAAAA=
In addition to using RAUSHAN KUMAR's answer, you can also use InterventionImage like this
Route::get('test', function() {
$image = 'data:image/webp;base64,UklGRlIFAABXRUJQVlA4IEYFAAAQHACdASpPAHkAPlEQlEojkdHMYDgFBLIAZqA25eJVGRUWLtSnk7iTzHecd0SXUx8+t+zLWt8WJpEWrTTvLe9y9CX9VSAw3YOVEdn4oGm0ZIrnQUIJ7VsI/r+aW0VOJeFoJylth8MmFQHlbPEklNUVbgyVJnINmgXnDbtgj9paOvkDYnVAdv2ErJONHxXyp2eyn7mB6vcVu9AfWuUtDlYxhQG1CEongtTYR0U4jQIbXYTTzN/3G5cAEd0FmVN272q9XHtEAeWzrMHHsZ7YPtpmppalPe8BvhnrwSA+ctaG9iQ6b7pEVBVPAAD+/uy97OjEKfy53WiZp+vshoaLbmP0cVKb4k6hnKsxJdcP+CgaRYQkOjb4FGLJO55Q/c+afu5UzMOW3Tx4pq6YezZD8PSoPz4zk6GAAEL9XDf3c3RwQKG1r2lWKnBonjqCV8/oU/xr4Gv59yLHfFPfneUb6BrG9yoc40NAk+xGkgtKGsIDDanX+uuhaKWGntbgweNVylzqaIqZrCMYGgfkbTo+yPQ0JgHev/+hCnqRe4cEi4VfveeAi+7wBLg2w4tZOj0d7O7gJM6Zj9uaLB6l/3xyvdHwzThmi8na5GMB/v+Y7YAIYCGOV62mQ6XSrBMQHKUoPvIVwUeHVkJF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Y6Thjn0W8zAWnd3VwwB3ZS/Uv/ubq8H7W9He6fvg6Ib/101yfwV8xpmLHwZGmXWgAAAAAAA=';
$image = imagecreatefromwebp($image);
return Image::make($image)->resize(100)->response();
});
By calling the route 'test' you will see the image.
As this is a webp type image, so i need to use imagecreatefromwebp() to upload the images. I have written this piece of code for that.
$destinationFolder = 'uploads/';
$folderName = $folder . '_' . $adId;
if ($folderName != '') {
$folderNames = explode('_', $folderName);
$folderPath = implode('/', array_map(function ($value) {
return $value;
}, $folderNames));
$destinationFolder .= $folderPath . '/';
}
$destinationPath = storage_path($destinationFolder);
if (!\File::exists($destinationPath)) \File::makeDirectory($destinationPath, 0777, true, true);
$fileName = $folder . '_' . $adId . '_0_' . time() . '.jpg';
$fileName = ($fileName != '') ? $fileName : $folderName . '_' . time() . '.jpg';
$im = imagecreatefromwebp($data);
$imageResult = imagejpeg($im, $destinationPath . $fileName, 100);
imagedestroy($im);
if ($imageResult) return '/image/' . $fileName;
return "/DefaultImage.jpg";
If you still wants to use your code, you can remove data:image/webp;base64, then use base64_decode after you remove it.
$image=explode(",",$postData['image']);
$image=base64_decode($image['1']);
Image Intervention can decode your base64 image you can try this
$imageResult = Image::make($postData['image'])->resize($imageWidth, $imageHeight, function ($constraint) {
$constraint->aspectRatio();
I have some problem in this code. This code is working on my local windows based system. But the same code in not working on online server. I have a function that convert the big images in to small size images according to the parameters. the code of this function is given below.
public function do_resize($source_file, $target_folder, $height = 128, $width = 128){
$filename = $source_file;
$temp_data = explode('/',$filename);
$new_filename = end($temp_data);
$temp_data = explode('.', $new_filename);
$ext = end($temp_data);
$new_filename = $temp_data[0] . $width .'-'. $height .'.'. $ext;
$source_path = $filename;
$folder_path = '';
$temp_folder = explode('/',$target_folder);
foreach ($temp_folder as $folder) {
$folder_path .=$folder . '/';
if (!file_exists($folder_path)) {
mkdir($folder_path);
}
}
$target_path = $target_folder;
if (file_exists($source_file)) //file_exists of a url returns false.It should be real file path
{
return $folder_path . $new_filename;
}
if(isset($config_manip)){
unset($config_manip);
}
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'maintain_ratio' => FALSE,
'new_image' => $target_path,
'create_thumb' => TRUE,
'thumb_marker' => $width . '-'. $height,
'width' => $width,
'height' => $height
);
$CI =& get_instance();
$CI->load->library('image_lib');
$CI->image_lib->initialize($config_manip);
if (!$CI->image_lib->resize()) {
echo $CI->image_lib->display_errors();
echo "<br>";
echo $config_manip['source_image'];
}
// clear //
$CI->image_lib->clear();
return $folder_path . $new_filename;
}
I am calling this function like this.
$name = $this->imageresize->do_resize($brand['logo'], $target_folder, 50, 50);
Then input parameter $brand['logo'] have this value "uploads/images/system/placeholder.png" and parameter $target_folder have this value "uploads/images/cache/brands/brand-logo".
I did not get any error from this code. but It is not resizing the images also. I also set the permission of the directory to 777.
Any one have some solution for this. Thanks
Change:
if (file_exists($source_file)) //file_exists of a url returns false.It should be real file path
{
return $folder_path . $new_filename;
}
on
if (file_exists($folder_path . $new_filename)) //file_exists of a url returns false.It should be real file path
{
return $folder_path . $new_filename;
}
AND $config_manip:
'new_image' => $target_path,
ON
'new_image' => $folder_path . $new_filename,
:)
i want to upload my images with their own names. But when i tried, they upload with diffrent names. for example; php0K0Saj.57352.JPG (!?)
my controller;
public function post_Savenews(Request $request)
{
$request->all();
/* out of question
$head = $request->input('head');
$content = $request->input('content');
$keywords = $request->input('keywords'); */
$featured=$request->post_featured;
$extension=$request->post_featured->getClientOriginalExtension();
$photoName = $featured . '.' . rand(11111, 99999) . '.' . $extension;
$request->post_featured->move(public_path('uploads'), $photoName);
News::create(array('head' => $head, 'content' => $content, 'keywords' => $keywords,'post_featured'=>$photoName));
return redirect()->route('index');
}
Update the line.
$photoName = $featured . '.' . rand(11111, 99999) . '.' . $extension;
With
$photoName = $request->post_featured->getClientOriginalName();
Hope this helps
how to use pdf2htmlEX to convert pdf file to html file in php
here is the link :-
https://github.com/coolwanglu/pdf2htmlEX
if any body know please help
thanks in advance.
Was working on this got it working, sharing the code so it might help someone :)
It requires pdf2htmlEX and pdftocairo on linux to be installed in order to work.
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$allowedExt = array('pdf');
if (in_array(strtolower($ext), $allowedExt)) {
$upload_file = time() . '.pdf';
$upload_file = str_replace(' ', '_', $upload_file);
if (!file_exists('upload_directory')) {
mkdir('upload_directory', 0777, true);
}
if (move_uploaded_file($_FILES['file']['tmp_name'], 'upload_directory/' . $upload_file)) {
$unq_no = 1; //Can be from database entry
$file_name = $upload_file;
$pdf_path = 'upload_directory/' . $file_name;
$name = str_replace('.pdf', '', $file_name);
$save_path = 'upload_directory/' . $unq_no;
new Folder($save_path, true, 0777);
$pdf_thumb_save_path = $save_path . '/' . $name;
if (!file_exists($pdf_thumb_save_path)) {
mkdir($pdf_thumb_save_path, 0777, true);
}
shell_exec("pdf2htmlEX --dest-dir $save_path --embed cfi --fit-width 760 --hdpi 72 $pdf_path");
shell_exec("pdftocairo -png -singlefile $pdf_path $pdf_thumb_save_path");
if (file_exists($save_path . '/f1.woff')) {
shell_exec("/path/rename_font.fs $save_path/f1.woff f1 $save_path/");
}
if (file_exists($save_path . '/f2.woff')) {
shell_exec("/path/rename_font.fs $save_path/f2.woff f2 $save_path/");
}
if (file_exists($save_path . '/f3.woff')) {
shell_exec("/path/rename_font.fs $save_path/f3.woff f3 $save_path/");
}
if (file_exists($save_path . '/f4.woff')) {
shell_exec("/var/app/current/webroot/img/uploads/rename_font.fs $save_path/f4.woff f4 $save_path/");
}
if (file_exists($save_path . '/f5.woff')) {
shell_exec("/path/rename_font.fs $save_path/f5.woff f5 $save_path/");
}
if (file_exists($save_path . '/f6.woff')) {
shell_exec("/var/app/current/webroot/img/uploads/rename_font.fs $save_path/f6.woff f6 $save_path/");
}
if (file_exists($save_path . '/f7.woff')) {
shell_exec("/path/rename_font.fs $save_path/f7.woff f7 $save_path/");
}
if (file_exists($save_path . '/f8.woff')) {
shell_exec("/path/rename_font.fs $save_path/f8.woff f8 $save_path/");
}
$base_folder_path = 'uploads/pdfs/html/' . $unq_no . '/';
$file_path = $base_url . $base_folder_path . $name . '.html';
$css_path = $base_url . $base_folder_path . $unq_no . '/';
chmod($pdf_thumb_save_path, 0777);
$current_data = file_get_contents($file_path);
$modified_data = str_replace('"stylesheet" href="', '"stylesheet" href="' . $css_path, $current_data);
$file_handle = fopen($pdf_thumb_save_path . '.html', 'w');
fwrite($file_handle, $modified_data);
fclose($file_handle);
}
}
Have you had a look at the wiki?
See for example: https://github.com/coolwanglu/pdf2htmlEX/wiki/Quick-Start
i used Scribd Platform API and its easy to implement and best well.
Thnx