Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to add attributes to html() method #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,17 @@ public function __toString()
/**
* Returning basic html code for this image
*/
public function html($title = '', $type = 'jpg', $quality = 80)
{
return '<img title="' . $title . '" src="' . $this->cacheFile($type, $quality) . '" />';
public function html($title = '', $type = 'jpg', $attributes = array(), $quality = 80)
{
$html = array();
if (!empty($attributes)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you do this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want to set any attribute, only want to change the quality of image you can pass null and empty array or no array at all. That will just change the quality of image and no attribute are added to the output.
Eg: html('title','jpeg','',20)) or html('title','jpeg',NULL,20)) or html('title','jpeg',array(),20))

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but since the foreach loop will not have any effect in this case the test is not useful

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreach is not execute only when you pass the NULL , if you pass an empty array or and empty string it will executed at least one time

foreach ((array)$attributes as $key => $value) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should typehint array in the prototype instead of casting it here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array required, with type casting you can pass things without array too.
html('title','jpeg','something',20))
that will output the
<img title="title" src="" something="something"/>
may be comes handy for any javascript plugin

if (is_numeric($key)) $key = $value;
if (!is_null($value)) $key . '="' . $value . '"';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line doesn't do anything, because $key . '="' . $value . '"'; has no effect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this line has no effect agreed.

$html[] = $key . '="' . ($value) . '"';
}
}
return '<img title="' . $title . '" src="' . $this->cacheFile($type, $quality) . '" ' . implode(' ', $html) . '/>';
}

/**
Expand Down