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,
:)
Related
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+afu5UzMOW3Tx4pq6YezZD8PSoPz4zk6GAAEL9XDf3c3RwQKG1r2lWKnBonjqCV8/oU/xr4Gv59yLHfFPfneUb6BrG9yoc40NAk+xGkgtKGsIDDanX+uuhaKWGntbgweNVylzqaIqZrCMYGgfkbTo+yPQ0JgHev/+hCnqRe4cEi4VfveeAi+7wBLg2w4tZOj0d7O7gJM6Zj9uaLB6l/3xyvdHwzThmi8na5GMB/v+Y7YAIYCGOV62mQ6XSrBMQHKUoPvIVwUeHVkJFWnUCt6S7yOMa9RkZxe8//Bphx4NhJ/dXc3x7HQESKmLUu8nofAJKiyg7v46s90BuZWpbVYysGbdPR9Shc9nqgYoEazEu+ik00Mr+VLM+/lS8aCumf4on0FkZ/Dn4SGJSU8pc02nt7ncW0e0XwVKx9DE8RfVww8GDv33+1ib7qkv1gsaGBdn60MpW2PzyI1ZDReCh25f4z4RsG91nEpjDr4MmVOaW40nXwNnAfuYawSt+b05IQx9GGw0seGFDJ4hbb+tTatszMiOSjhQ9HsO19t/hVFojVco/cKoG9XUSorPouBOqFqDrciO3+BVFFo5l5JW3Ka0ZtamSCrzt1AUzOndTy82imvJ+NZ1D+iXF92d3XITYsveniLVxEjp+pQIp8pXJ3p3DuFQPxzuQ44E3xWcPimJ7wuJnIrIm8jyFaM4AHJ1OBc/BG+0iP5zUHWl36LGK5VpoDkw34T+sQs9s0gOpG+tNM0uaKmwmONjo2L0tFqaiC4V4aHLO3JqptxoxSjn3BQVG5x/ga/7bz/hqeycRIoGrTuIqKAdCVVrYLEgr39NID+sKzK6BYzr8j9r3JMPZ3+2T09lEwe0u+S7B4wI3rlSz5L3DZspwtfkWcGtZpUCRqgOiWnFnpdIlor1+zvQK2ksDUpf0UbyRW/c59RQIfouYA6cKJUxsQzPPL5yvuIprzjZLY2HRkKFbemgUbPH1nxw28qtf2EixENC3uGus24PjYW8Jz/G5kZ2ioG2UXp00Aru2Z1Hk4AB0G2RPAzhdp1WQEnbqXb5a35eSSv5SGUYQbRDbaQNnyeODfb80a056/Cz7wmTtn5xvWT1UhTg7J/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,UklGRlIFAABXRUJQVlA4IEYFA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FaM4AHJ1OBc/BG+0iP5zUHWl36LGK5VpoDkw34T+sQs9s0gOpG+tNM0uaKmwmONjo2L0tFqaiC4V4aHLO3JqptxoxSjn3BQVG5x/ga/7bz/hqeycRIoGrTuIqKAdCVVrYLEgr39NID+sKzK6BYzr8j9r3JMPZ3+2T09lEwe0u+S7B4wI3rlSz5L3DZspwtfkWcGtZpUCRqgOiWnFnpdIlor1+zvQK2ksDUpf0UbyRW/c59RQIfouYA6cKJUxsQzPPL5yvuIprzjZLY2HRkKFbemgUbPH1nxw28qtf2EixENC3uGus24PjYW8Jz/G5kZ2ioG2UXp00Aru2Z1Hk4AB0G2RPAzhdp1WQEnbqXb5a35eSSv5SGUYQbRDbaQNnyeODfb80a056/Cz7wmTtn5xvWT1UhTg7J/9J/4cCoBkweCOc1exDC4bBpdmwNyB82TfMOL+HcsO/UR1EDkEBdEl2HLQOA2mSntOQ3dTgMQX/71+MtpetptbThjL/fnRMMfVnpovAe2jd4SXx+s8fMNJXcEkO9ZKxIWFH0EwLvzIp8SPc5z+AzLEwfn4aBvVL835u4BsodI+usLpWnlC7Xdz6JMVxKivRpoWgrcIsAF91564sRuvXd3rwozLc0Fb/at7P3B3B5Jc7wPXQ/CH5Y6Thjn0W8zAWnd3VwwB3ZS/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'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
{
}
I'm using laravel 4.2 image intervention in ubuntu 32 bit. It's working, but image is not resizing for the specified height. Suppose I'm trying to resize image of width=800, height=600 to width=250, height=250 but it is resizing to width=250, height=188 .
I used the following code in my controller.
$imageType = array(
'detail_page' => array(
'width' => 250,
'height' => 250
),
);
$file = Input::file('album_image');
if($file->isValid()) {
$file_name = microtime();
$file_name = str_replace(' ', '_', $file_name);
$file_name = str_replace('.', '_', $file_name);
$file_name = $file_name . '.' . $file->getClientOriginalExtension();
$file->move(public_path() . '/album_uploads/', $file_name);
foreach ($imageType as $key => $value) {
$file = Image::make(sprintf('album_uploads/%s',$file_name))->resize($value['width'], $value['height'],
function($constraint) {
$constraint->aspectRatio();
});
$file->save(public_path().'/album_uploads/'.$value['width'].'X'.$value['height'].'/'. $file_name);
}
$album_image_url = URL::to('album_uploads/' . $file_name);
}
Remove your aspect ratio constraint: 800x600 -> 250x188 -> 4x3
function($constraint) {
$constraint->aspectRatio();
}
I'm trying to add watermark on uploaded image, but it doesn't works.. There is my code, I can't see any errors there..
$data['photo_name'] = $data['slogan'];
$data['photo_ext'] = end(explode('.', $photoValue));
$data['photo_name'] = $data['photo_name'] . '.' . $data['photo_ext'];
file_put_contents($_SERVER["DOCUMENT_ROOT"] .'/'. PHOTOS_UPLOAD_FOLDER . $data['photo_name'], file_get_contents($photoValue));
// $this->resize_image($_SERVER["DOCUMENT_ROOT"] .'/'. PHOTOS_UPLOAD_FOLDER . $data['photo_name']);
$this->simpleimage->load($data['photosData']['uploadFolder'] . $data['photo_name']);
$this->simpleimage->cutFromCenter(450, 450);
$this->simpleimage->save($_SERVER["DOCUMENT_ROOT"] .'/'. PHOTOS_UPLOAD_FOLDER . $data['photo_name']);
// add watermark
$configImg = array();
$configImg['image_library'] = 'gd2';
$configImg['wm_type'] = 'overlay';
$configImg['source_image'] = $_SERVER["DOCUMENT_ROOT"] .'/'. PHOTOS_UPLOAD_FOLDER . $data['photo_name'];
$configImg['wm_overlay_path'] = base_url('assets/logo.png');
$configImg['wm_opacity'] = '50';
$configImg['wm_vrt_alignment'] = 'middle';
$configImg['wm_hor_alignment'] = 'center';
$this->image_lib->initialize($configImg);
$this->image_lib->watermark();
$this->image_lib->clear();
$this->image_lib->display_errors();
print_r($configImg);
and it does not printing any errors.
it prints only my array
Array
(
[image_library] => gd2
[wm_type] => overlay
[source_image] => ../king-include/uploads/possibly-the-best-youtube-comment-ever-.jpg
[wm_overlay_path] => http://www.domain.info/parser/assets/logo.png
[wm_opacity] => 100
[wm_vrt_alignment] => top
[wm_hor_alignment] => center
)
source_image : Sets the source image name/path. The path must be a relative or absolute server path, not a URL.
The problem is with your watermarking overlay image. Set it with correct path and try again.
I want to upload a resized image to Amazon S3 bucket, using Yii framework, but to do it directly -- without uploading original (not resized) image to any folder, anywhere within Yii, website or server structure.
I have used ThumbsGen extension to create thumbnail of an image. The code works, if I upload file on my own server. But, if I upload the image to S3, then it will not create a thumbnail.
My code is look like this:
<?php
Yii::app()->setComponents(array('ThumbsGen' => array(
'class' => 'ext.ThumbsGen.ThumbsGen',
'thumbWidth' => Yii::t('constants', 'SECRETCODE_WIDTH'),
'thumbHeight' => Yii::t('constants', 'SECRETCODE_HEIGHT'),
'baseSourceDir' => Yii::app()->request->s3baseUrl.'/uploads/secretcode/original/',
'baseDestDir' => Yii::app()->request->s3baseUrl. '/uploads/secretcode/thumbs/',
'postFixThumbName' => null,
'nameImages' => array('*'),
'recreate' => false,
)));
class SecretCodeController extends MyController {
public function actionCreate() {
$model = new SecretCode;
$model->scenario = 'create';
if (isset($_POST['SecretCode'])) {
$model->attributes = $_POST['SecretCode'];
$temp = $model->promo_image = CUploadedFile::getInstance($model, 'promo_image');
if ($model->validate()) {
$rnd = rand(1, 1000);
$ext = $model->promo_image->extensionName;
$filename = 'secret_' . Yii::app()->user->app_id . '_' . time() . '_' . $rnd . '.' . $ext; $success = Yii::app()->s3->upload( $temp->tempName , 'uploads/secretcode/original/'.$filename);
if(!$success)
{
$model->addError('promo_image', Yii::t('constants', 'IMAGE_FAILURE'));
} else {
$model->promo_image = $filename; $model->promo_image = $filename;
$fullImgSource = #Yii::app()->s3->getObject(Yii::app()->params->s3bucket_name,"uploads/secretcode/original/".$filename);
list($width, $height, $type, $attr) = getimagesize($fullImgSource->headers['size']);
$dimensions = array();
$dimensions = CommonFunctions :: SetImageDimensions($width, $height,Yii::t('constants', 'SECRETCODE_WIDTH'), Yii::t('constants', 'SECRETCODE_HEIGHT'));
Yii::app()->ThumbsGen->thumbWidth = $dimensions['width'];
Yii::app()->ThumbsGen->thumbHeight = $dimensions['height'];
Yii::app()->ThumbsGen->createThumbnails();
}
if ($model->save()) {
Yii::app()->user->setFlash('success', Yii::t('constants', 'Secret Code')." ". Yii::t('constants', 'ADD_SUCCESS'));
$this->redirect(array('create'));
}
}
}
}
$this->render('create', array(
'model' => $model,
));
}
}
As a result, I'm getting a PHP warning:
getimagesize(42368) [<a href='function.getimagesize'>function.getimagesize</a>]: failed to open stream: No such file or directory
What am I doing wrong?
getimagesize — Get the size of an image
array getimagesize ( string $filename [, array &$imageinfo ] )
Your code:
list($width, $height, $type, $attr) = getimagesize($fullImgSource->headers['size']);
$fullImgSource->headers['size'] - I doubt that it stores the file path to image;
try this:
list($width, $height, $type, $attr) = $fullImgSource->headers['size'];
or
list($width, $height, $type, $attr) = getimagesize(/*path to image*/);
Can also read Transferring Files To and From Amazon S3