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);
Related
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
I tried most of the suggestions at the below links (1.53 image scale and larger images)
tcpdf: poor image quality
TCPDF image quality issues
I am printing a 5x7 greeting card with a front, back, and inside image. The image source files are pngs/jpegs (tried both).
If you look at images below and PDF result; the PDF looks washed out.
CODE: (1.53 image scale in global config)
public function build_design(Design $design)
{
PDF::SetTitle('Design');
PDF::setJPEGQuality(100);
PDF::AddPage();
//Needed for images not being right size https://stackoverflow.com/questions/8135975/why-is-tcpdf-image-smaller-than-it-should-be
PDF::SetAutoPageBreak(false, 0);
if ($design->get_image_by_name('Back'))
{
PDF::Image($design->get_image_by_name('Back')->image,0,0,5,7);
}
if ($design->get_image_by_name('Front'))
{
PDF::Image($design->get_image_by_name('Front')->image,5,0,5,7);
}
if ($design->get_image_by_name('Inside'))
{
PDF::AddPage();
PDF::Image($design->get_image_by_name('Inside')->image,0,0,10,7);
}
}
public function print_design(Design $design)
{
$this->build_design($design);
PDF::Output('design.pdf');
}
RESULTING PDF:
https://www.dropbox.com/s/w10vq8q48m0ljtt/card.pdf?dl=0
FRONT:
1500x2100 300 dpi PNG
INSIDE
3000x2100 300 dpi PNG
BACK:
1500x2100 300 dpi JPEG
I am using yii\imagine\Image extension and want to add a watermark to my image.
Here's my code:
$watermarkImage = '#webroot/../images/watermark.png';
$image = '#webroot/../slike/img-4.jpg';
Image::watermark($image, $watermarkImage);
After this code is executed, nothing happens. What am I missing here?
The ::watermark() function creates the new image but doesn't automatically saves it. The function returns a Imagine\Gd\Image object. This object can be used to save the new files.
$watermarkImage = '#webroot/../images/watermark.png';
$image = '#webroot/../slike/img-4.jpg';
// Store the Image object in a variable
$newImage = Image::watermark($image, $watermarkImage);
// Call the save function to write the file to the disk.
$newImage->save(Yii::getAlias('#webroot/../slike/img-4-watermark.jpg'));
I'm trying to figure out the correct use of the Header(content-type:image/png) .I have an upload script that uploads and resizes correctly,with or without the header, but it seems like its recommended to have the header .Problem is that with the header, once the script completes, it throws a 'broken img' icon in the top left corner of the window and exits the script. How do I make it behave differently like if I want to redirect to a new page. Adding Header(location:...) at the end of the script doesn't seem to make a difference. ANY Help is appreciated.
<?PHP
/*image resize with Imagick*/
function imageResize($image){
/*call to imagick class and resize functions will go here*/
echo 'the image to be resized is : '.$image;
$newImage=new Imagick();
$newImage->readImage($image);
$newImage->resizeImage(1024,768,imagick::FILTER_LANCZOS, 1);
$newImage->writeImage('myImage.png');
$newImage->clear();
$newImage->destroy();
}
/*image resize with GD image functions*/
function imgResize($image){
header('Content-Type: image/png');
$newWidth='1024';
$newHeight='768';
$size=getimagesize($image);
$width=$size[0];
$height=$size[1];
//return $width;
$dest=imagecreatetruecolor($newWidth,$newHeight);
$source=imagecreatefrompng($image);
imagecopyresized($dest,$source,0,0,0,0,$newWidth,$newHeight,$width,$height );
return imagepng($dest,'./users/uploads/newimage.png');
imagedestroy($dest);
}
/*Function that actually does the upload*/
function file_upload(){
if(!empty( $_FILES) ){
print_r($_FILES);
echo '<hr>';
$tmpFldr=$_FILES['upFile']['tmp_name'];
$fileDest='./users/uploads/'.$_FILES['upFile']['name'];
if(move_uploaded_file($tmpFldr,$fileDest)){
echo 'Congratulations, your folder was uploaded successfully <br><br>';
}
else{
echo 'Your file failed to upload<br><br>';
}
return $fileDest;
} else{die( 'Nothing to upload');}
} /*End file upload function */
$fileLocation=file_upload();
echo 'location of the new file is : '.$fileLocation.'<hr>';
$newImage=imgResize($fileLocation);
?>
You can't serve an image and serve a redirect at the same time. If you want to output a PNG image then you output the Content-Type header and that's exclusive of the Location header.
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 :)