Ignore `OutOfBoundsException` error when watermark grower of source image - image

In Yii2 and core yii\imagine\Image class, When use this command:
Image::watermark('image.jpg', 'watermark.jpg')->save('image.jpg');
If dimension of watermark, grower of source image, return this error:
Exception 'Imagine\Exception\OutOfBoundsException' with message 'Cannot paste image of the given size at the specified position, as it moves outside of the current image's box'
It's fine. But how can ignore this error, so new image will be create but the part of watermark that outside of the image box is hidden and removed.

There is no ignoring the Exceptions thats why they are called by that name, The Exception is thrown from the paste(ImageInterface $image, PointInterface $start); method
$size = $image->getSize();
if (!$this->getSize()->contains($size, $start)) {
throw new OutOfBoundsException('Cannot paste image of the given size at the specified position, as it moves outside of the current image\'s box');
}
that is called in the Image::watermark function. It is restricted and not allowed so you cannot Ignore it.
You can ignore/suppress notices or warnings but you can catch the exceptions and do something to fix it or present the error to the user
try{
\yii\imagine\Image::watermark ( 'img/image.png' , 'img/logo.jpg' );
} catch (\Imagine\Exception\OutOfBoundsException $ex) {
Yii::$app->session->setFlash('error',$ex->getMessage()) ;
}
Or if the exception occurs resize the watermark image smaller than the base image and call the Image::watermark again.
EDIT
You can do something like below what i was talking about suppose you have an actionWatermark() where you are creating the watermark image
public function actionWatermark() {
$image='image.jpg';
$watermark= 'watermark.jpg';
try {
//call watermark function
$this->watermark ($image,$watermark);
} catch ( \Imagine\Exception\OutOfBoundsException $ex ) {
$img = \yii\imagine\Image::getImagine ();
//get the base image dimensions
$size=$img->open ( $image )->getSize();
//resize the watermark image
$resized=\yii\imagine\Image::resize ( $watermark , $size->getWidth () , $size->getHeight (),TRUE);
//call again
$this->watermark ($image,$resized);
}
}
private function watermark($image,$watermark) {
\yii\imagine\Image::watermark ( $image , $watermark )->save ( 'image.jpg' );
}

Related

Images not showing in update function using BackPack for Laravel

I´m saving images in my crud. The url is being stored in the DB correctly, however the image is not appearing when I try to edit the element. The image shows like this:
I'm using this mutator:
public function setImageAttribute($value)
{
$evidencia = "image";
$disk = "public";
$destination_path = "public/uploads/evidenciaQuejas";
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$evidencia});
// set null in the database column
$this->attributes[$evidencia] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \Image::make($value)->encode('jpg', 90);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the path to the database
$this->attributes[$evidencia] = $destination_path.'/'.$filename;
}
}
Also, the folder that I created in the server to store the images ("public/uploads/evidenciaQuejas") says it has 0 archives inside even though the image is being stored without errors.
Could you help me please?

How to display a png image from api response in flutter?

I'm getting a PNG image (NOT URI) as a response to my api request as shown bellow:
Future PostToGetPostImages(String pic_id,String pic_type) async {
try {
var parms = {
'code': pic_type+"_"+pic_id,
};
Response response = await _dio.post(_get_post_image,
data: parms,
options: Options(contentType: Headers.formUrlEncodedContentType),
onSendProgress: (int sent, int total) {
print('sent : $sent // total : $total');
}, onReceiveProgress: (v, s) {
print('v : $v // s : $s');
});
print('***** Response body Download Profile Images: ${response.data} **********');
return response.data;
} catch (error, stacktrace) {
_handleError(error);
print("Exception occured A: $error stackTrace: $stacktrace");
}
}
This is what it print after execution:
I/flutter (12777): ***** Response body Download Profile Images: �PNG
I need to know how to display this response ( the png image ) in flutter, I'm using this code but it's not working:
FutureBuilder(
future: aptProvider().PostToGetProfileImages("1", "m"),
builder: (context, snapshot) {
return CircleAvatar(
radius: 65.0,
backgroundImage: backgroundImage: NetworkImage(snapshot.data
.toString()));
}),
The problem here that you is not using a URL image, confirm to use a URL end with (.png, jpg, ..etc.) and also confirm it by open it in your browser.
then
to display it in you widget use one of this ways
First
Image.network('URL')
// ex: Image.network('https://picsum.photos/250?image=9')
Second
Container( height : 100 , width :100 , decoration : Box decoration (
image : Decoration image ( Network image ('URL'), ),
), ), ),
Perhaps the png is like a dummy image like a 404 like the one in the image attached so you have to copy and try check it with Image.network if everything works fine you proceed with image.network
It seems like you get the image as a byte array, base64 string.
Here is how I solve that.
var base64 = response.split(';base64,')[1];
base64 = base64.replaceFirst('binary data/', '');
Uint8List bytes = base64Decode(base64);
// Image Widget which you can put in anywhere.
Image.memory(bytes, fit: BoxFit.contain)
I'm not sure if you got it to work but I wanted to leave this for future reference:
You are receiving raw bytes as part of the response; from there you will need to use those to create an image. For this you have two options:
Save the bytes to a file.
Show the image bytes straight from memory.
Option 1 is usually what you want, but if you need a quick test you can do option 2 without issues, keep in mind memory is limited. Both have different implementations depending on the packages you are using.
Here's a quick overview of what Option 2 might look like using just plain old http:
// MyRepository.dart
class Repository {
Future<Uint8List?> getImageBytes(<...some params>) async {
final streamResponse = await client.send(<...some req>);
final response = await Response.fromStream(streamResponse);
if (response.statusCode == 200) {
parsedResponse = response.bodyBytes;
}
return null;
}
}
// MyWidget.dart
class MyWidget extends StatelessWidget {
<... some other code>
final Uint8List imageRawBytes;
#override
Widget build(BuildContext context) {
return Image.memory(
imageRawBytes,
);
}
}
What's important here is that you use the raw response bytes instead of using a response body (which is usually an encoded String). This way you can build either a File or keep them in memory to be used.
If you are getting the image url from the response, then you can use the Image.network(imageUrl)
OR
If you are getting the imageData in the form of Uint8List, then you can use the Image.memory(imageData)

How ImageMagick readimage from a relative path WampServer64

I'm testing the php_imagick version 3.4.4.7.4 against a WampServer64 installation using
<?php
/* Create the Imagick object */
$im = new Imagick();
/* Read the image file */
try {
$im->readImage( "test.jpg" );
} catch( \ImagickException $e ) {
die( $e );
}
/* Thumbnail the image ( width 100, preserve dimensions ) */
$im->thumbnailImage( 100, null );
/* Write the thumbail to disk */
$im->writeImage( 'th_test.png' );
/* Free resources associated to the Imagick object */
$im->destroy();
It results in raised ImagickException No such file or directory. The file sits in the same folder as the php file.
When I replace 'test.jpg' by an absolute path the code works but the resulting file at writeImage ends up in the folder C:\wamp64\bin\apache\apache2.4.41
Is this normal behaviour? I don't see a remark in https://www.php.net/manual/en/imagick.readimage.php that paths should be absolute. Is this a quirk in WampServer64?

How to dynamically load images in a layer using actionscript

I used the below code to load images that are present in my library:
Create_Image_With_Description();
function Create_Image_With_Description():Void {
this.createEmptyMovieClip("imageHolder", 600);
// Put Here Images URoL
imageHolder.loadMovie("Symbol 1");
// Position
imageHolder._x = 0;
imageHolder._y = 0;
// Size
imageHolder._xscale = 210;
imageHolder._yscale = 215;
// Example: my_mc.createTextField(instanceName:String, depth:Number, x:Number,
y:Number,width:Number, height:Number) : Void
this.createTextField("description1Field",1,10,203,120,20);
// Put Here Images description
description1Field.text = "Resident Evil: Afterlife";
}
Here symbol 1 is the image that i've converted into a symbol. When i tried running this i got an error saying "Error opening URL 'file:///E|/Symbol 1'".
I've also tried giving imageHolder.loadMovie("img1.jpg"); But still it didn't worked. Can anyone help me with this
Try replacing these lines:
this.createEmptyMovieClip("imageHolder", 1000);
// Put Here Images URL
this.attachMovie("img1", "img1", {_x:100, _y:100});

Actionscript 3. Display alternative images (using conditionals) when mp3 available or not-available to play

I wondering the best way to handle this
I'm successfully loading sounds (audioMP3) from an XML file and handling the IO errors with EventListener.
I would like to have an image show on stage, when an MP3 is available or an alternative image when there is no MP3.
I've been trying to access the IO error and use it in conditionals to select the image e.g.
If there is an IO error then display btnAudioNo
Else display btnAudioYes
Here's the eventListemer:
audioMP3.addEventListener(IOErrorEvent.IO_ERROR, onSoundIOError, false, 0, true);
function onSoundIOError (e:IOErrorEvent){
trace(e.text);
removeEventListener(IOErrorEvent.IO_ERROR, onSoundIOError)
}
And my dodgy conditional attempt:
var btnAudioYes:Bitmap = new Bitmap(new(getDefinitionByName("btnAudioYes")) (0,0) );
var btnAudioNo:Bitmap = new Bitmap(new(getDefinitionByName("btnAudioNo")) (0,0) );
if(ioError = false){
addChild(btnAudioYes);
}
else {
addChild(btnAudioNo);
}
My questions are, how can I get this to work and is there a better way to determine if there is an MP3 file available (in the XML file) and display the appropriate image?
Many thanks for your suggestions.
Listener (in addition to the IOErrorEvent) to the ProgressEvent, if you get progress then the file exists and you can cancel (close) the loader. Unless you want the entire audio file loaded at this point, then listen for the complete event instead.
loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onSoundProgress, false, 0, true);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSoundLoadComplete); //use this only if you want to load the entire audio file at this point
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onSoundIOError, false, 0, true);
loader.load("your file");
function onSoundIOError (e:IOErrorEvent){
//this function will only run if the file does not exist
loader = null;
var btnAudioNo:Bitmap = new Bitmap(new(getDefinitionByName("btnAudioNo")) (0, 0) );
addChild(btnAudioNo);
}
function onSoundProgress(e:ProgressEvent) {
//this function will only run if the file DOES exist
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onSoundProgress); //we don't want this firing again
var btnAudioYes:Bitmap = new Bitmap(new(getDefinitionByName("btnAudioYes")) (0,0) );
addChild(btnAudioYes);
//if you you don't want to actually load the audio file, do this to cancel the load
loader.close(); //close the loader to keep from loading the rest of the file
loader.contentLoaderInfo.unloadAndStop(true);
loader = null;
}
//use this only if you want to load the entire audio file at this point
function onSoundComplete(e:Event):void {
//do whatever you need to do with the sound...
}

Resources