GithubHelp home page GithubHelp logo

Comments (8)

jenstornell avatar jenstornell commented on June 3, 2024 2

One idea that comes to mind is this...

$override = false; // Default is true

echo \ImageConverter\convert($from , $to , $quality, $override);

...but because both $quality and $override are optional I don't really like it. What if I don't want to specify the quality argument?

@peter279k If the file is saved successfully, I think it should return true no matter if an image already exists or not. That's because true means that the operation was successful.

If the $override is false, it should not override an already existing file and because that operation will fail it should return false. On error somewhere throw an exception.

from php-image-converter.

jenstornell avatar jenstornell commented on June 3, 2024 1

I'm just throwing in another concept in here.

echo \ImageConverter\convert($from, $to);
echo \ImageConverter\convert($from, $to , $quality = 80);
echo \ImageConverter\convert($from, $to , $args = ['quality' => 80, 'override' => 'false']);

With this concept it works just as now, BUT if the third and only optional argument is an array, treat it as an array with options.

from php-image-converter.

jenstornell avatar jenstornell commented on June 3, 2024 1

@peter279k I agree.

A day has gone and now I get confused about the override return.

If override is false and a file already exists, it should not override the file, but is it successful or not? It can be successful, because it did not convert the image file BUT it can be successful because the expected behaviour can be that it should just skip the file if it already exists and then move on.

Maybe I'm overdoing it but this is another idea:

echo \ImageConverter\convert($from, $to , $args = [
  'override' => false,
  'debug'    => true
]);

In case of debug is true it can return an array like below:

[
  'success' => true,
  'file_overwritten' => true,
  // from path
  // to path
  // from format
  // to format
  // from filesize
  // to filesize
  // filesize diff
]

from php-image-converter.

jenstornell avatar jenstornell commented on June 3, 2024

Before adding a file_exists in there, test the code without it and see what happends. Maybe exif_imagetype will take care of it?

from php-image-converter.

peter279k avatar peter279k commented on June 3, 2024

Well, we have the two options to check whether the targeted file is existed.

  • Using file_exists before saving/overriding the image file.
  • Using the exif_imagetype to detect the image file type before saving/overriding image file.

I think these two approaches can choose one and I also notice that we have to detect the targeted image file is saved/override.

If saving the image file, what will we do for the behavior? Return the message string?

If overriding the image file, what will we do for the behavior? Return the message string?

from php-image-converter.

peter279k avatar peter279k commented on June 3, 2024

Hi @jenstornell, thank you for your recommendation.

It looks good. The method returns true when the image file is saved. Otherwise, it returns false when the file is override.

About the $quality argument, it's normal that the optional argument will be the last in specific method.

That is, consider following code snippets:

.......
function this_function($required, $required2, $option = 'option1', $option2 = 'option2')
......

You can also look at the native PHP function reference and you will know what I talk about :).

If we want to pass one of optional argument, perhaps we have to use the func_get_args and func_num_args to parse function argument lists to detect whether the $quality is defined.

We can check the second argument is integer or bool type.

If it's integer type, it defines the $quality variable.

Otherwise, the $quality variable is not defined.

from php-image-converter.

jenstornell avatar jenstornell commented on June 3, 2024

@peter279k Interesting ideas. I've done a type check in previous projects and it works but it can be messy, especially with three or four optional arguments. It needs to ask itself.

  1. Are there two optional arguments?
  2. Are there one optional argument?
  3. Which one argument should I use?

My suggestion is that we don't stress it with this one until there is a really clear and perfect way to solve it. Else we may end up changing the behaviour later and rewrite it.

from php-image-converter.

peter279k avatar peter279k commented on June 3, 2024

Hi @jenstornell, thank you for your reply.

I think this approach you mention is better than mine.

It's good for us to let this additional arg in convert function be array 👍 .

from php-image-converter.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.