Resize with PHPTumb - php

I've got a problem with PHPThumb, my resize function doesn't work.
This function must resize an image to the maxi dimension x or y of a DIV :
if($thumb_x_value>$thumb_y_value) {
$thumb->resize($thumb_x_value, $thumb_x_value)->save($url_fichier);
}
else {
$thumb->resize($thumb_y_value, $thumb_y_value)->save($url_fichier);
}
The image is not resized.
Is the solution here ? The path used is a the relative path, because the file is stored in an another folder, and the $url is
../../../uploads/image.jpg
Any idea ?

Enlarge image isn't allowed by default.
Edit GD Thumb.inc.php, line 710
into the SetOptions public function and set resizeUp false to true
$defaultOptions = array (
'resizeUp' => true
That's all

Related

After Uploading PNG image without background gets black background in Laravel. Please check

private function processImageUpload($image,$image_name){
//using image png function to change any image from jpeg to png and store to public folder
$img = imagepng(imagecreatefromstring(file_get_contents($image)), config('app.LOGO_MEDIA_PATH').'/'.$image_name.'.png');
/*
* TODO : change the image replacing functionality
* from static name to upload to uploads folder
* and get URL form settings table
*/
$url = "/front/common/img/".$image_name.'.png';
return $url;
}
I enable the imagick extension in php.ini file but didn't happen anything
I got the solution..Just need to add case with the image type. In this we can't convert png to png.
private function processImageUpload($image,$image_name){
if($image->getMimeType() !== "image/png"){
//using image png function to change any image from jpeg to png and store to public folder
$img = imagepng(imagecreatefromstring(file_get_contents($image)), config('app.LOGO_MEDIA_PATH').'/'.$image_name.'.png');
}else{
$image->move(config('app.LOGO_MEDIA_PATH'), $image_name.'.png');
}
/*
* TODO : change the image replacing functionality
* from static name to upload to uploads folder
* and get URL form settings table
*/
$url = "/front/common/img/".$image_name.'.png';
return $url;
}

How to remove background in images using intervention image

I'm trying to remove the white background in logos to get a transparent icon, for example
on this image
Is .jpg, I want to remove withe background (Only what's outside if is possible, otherwise removing all the white background)
I'm trying using laravel intervention image with this code
public function resizeImage($logo, $placeName, $domain)
{
$mask = Image::make($logo)
->contrast(100)
->contrast(50)
->trim('top-left', null, 40)
->invert(); // invert it to use as a mask
$new_image = Image::canvas($mask->width(), $mask->height(), '#000000')
->mask($mask)
->resize(32,32, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})
->encode('png', 100);
$route = $domain . '/favicon/favicon.png';
Storage::disk('gcs')
->put($route, $new_image);
$route = Storage::disk('gcs')
->url($route);
return $route;
}
but this is my result
the image lost color (black colored)
what am I doing wrong? I feel that I am very close to achieving it, however, I need the icons to have color
$img = Image::canvas(800, 600);
Call canvas method without background color.
See more on http://image.intervention.io/api/canvas

resize and replace image using PHP

I am trying to resize and replace an image uploaded by the user but the most I can do is resize and output as another file. I have used the library image magician to resize.
If someone can explain how I can do it without using the library it would be better.
public function add() {
$f=Base::instance()->get('FILES');
$fext=pathinfo ($f['usrimg']['name'],PATHINFO_EXTENSION);
$this->copyFrom('POST');
$this->save();
$newName=str_pad($this->_id,5,"0").'.'.$fext;
move_uploaded_file($f['usrimg']['tmp_name'], $newName);
$this->load('id='.$this->_id);
$this->set('photo',$newName);
$this->update();
require_once('php_image_magician.php');
$magicianObj = new imageLib($newName);
$magicianObj->resizeImage(100, 200);
$magicianObj->saveImage('q.jpg', 100);
}
If you want to use gd library, for example you can use this code for resizing jpeg images:
$image=file_get_contents('image.jpg');
$src=imagecreatefromstring($image);
$x=imagesx($src);
$y=imagesy($src);
$y1=100; //new width
$x1=intval($y1*$x/$y); //new height proprtional to new width
$dst=imagecreatetruecolor($x1,$y1);
imagecopyresized($dst,$src,0,0,0,0,$x1,$y1,$x,$y);
header("Content-Type: image/jpeg");
imagejpeg($dst);

CodeIgniter Image Lib class doesn't resize image but returns true

I'm trying to resize an image using codeigniter's image lib library. Pretty simple. However, the image isn't resized and moreover, the function returns true, display_errors() doesn't show any errors.
Here's my config that I'm passing to the image lib:
//A file is uploaded using codeigniter's upload library, then:
$imgData = $this->upload->data();
$config['image_library'] = 'GD';
$config['source_image'] = $imgData['full_path'];
$config['new_image'] = $imgData['full_path'];
$config['create_thumb'] = false;
$config['maintain_ratio'] = true;
$config['width'] = $newWidth;
$config['height'] = $newHeight;
A print_r of $config after its been set:
Array
(
[image_library] => GD
[source_image] => C:/wamp/www/uploads/8ddbfb2cce91ee314e1f296355aec8c6.jpg
[new_image] => C:/wamp/www/uploads/8ddbfb2cce91ee314e1f296355aec8c6.jpg
[create_thumb] =>
[maintain_ratio] => 1
[width] => 400
[height] => 350
)
The paths are correct, however the image remains untouched after resizing.
Doing a var_dump on $this->image_lib->display_errors() returns this:
string '' (length=0)
If it makes any difference, the images are being uploaded through Uploadify.
Any ideas on what's wrong?
Try to set the full path from your document root and not from C.
Try this code:
if($this->upload->do_upload()){
$this->load->library('image_lib');
$config_res['source_image'] = 'uploads/'.$this->upload->file_name;
$config_res['maintain_ratio'] = TRUE;
$config_res['width'] = $newWidth;
$config_res['height'] = $newHeight;
$this->image_lib->initialize($config_res);
$this->image_lib->resize();
$this->image_lib->clear();
}
else{
echo $this->upload->display_errors();
}
in this way you don't need to specify the new_image and it will be overwritten
Did you set create_thumb to an empty string? Looking at CodeIgniter source, setting this parameter to anything else than the boolean FALSE (which is the default value) is recognized as a value of "true".
The purpose of the create_thumb mode is to append a suffix to the name of the generated files, by default "_thumb". http://codeigniter.com/user_guide/libraries/image_lib.html
Maybe you have "filename_thumb.jpg" files in your uploads folder? If so, issue found ;) Just delete or fix your create_thumb parameter.
As a side note, maintain_ratio = 1 doesn't work, only TRUE works.
Did You Try To Avoid This Bugs on the create_thumb function ?
& Boolean in Capital Letter?
$config['create_thumb'] = FALSE;
$config['new_image'] = $this->image_thumb_name($img);
Reference: https://stackoverflow.com/a/9040862/1444072
make sure Note: In order for the image class to be allowed to do any processing, the folder containing the imags.**e files must have write permissions.
**Note: Image processing can require a considerable amount of server memory for some operations. If you are experiencing out of memory errors while processing images you may need to limit their maximum size, and/or adjust PHP memory limit

PHP temporary image and CodeIgniter view

I have .jpg images in path (/home/folder1/folder2/) to deny public access to the images. I'm using CodeIgniter and following function to show single image.
function show_image()
{
$image_path = '/home/folder1/folder2/photo2.jpg';
$image = file_get_contents($image_path);
header("Content-Type: image/png");
echo $image;
}
I would like to have solution where I can pass the resized image to CodeIgniter view and show it using tag.
function show_image2()
{
// TODO image load and resize
// loaded and resized image
$data['image'] = $image;
$this->load->view('show_image', $data);
}
View:
<img src="<?php echo $image; ?>" />
Do I need to make temporary copy of image to public path?
EDIT:
How can I make temporary copy of image? Source folder is "/home/folder1/folder2" and destination folder is "example.com/images". Image should be deleted when user changes the image.
Yes you do, unless you're passing the function the raw image data (which means you'll need to change show_image() a bit :)

Categories