0

I need to check if a K2 article image exist in a third party extension. If the image exist, it should be displayed otherwise, a default image should be fetched.

I am making the image path with :

$imagePath = "/media/k2/items/cache/".md5("Image" . $item->object_id )."_XS.jpg";

($item->object_id is the article id) when the image exists, it is displayed.

I have tried several methods to check if the image exists with :

if (file_exists('http://' . $_SERVER['SERVER_NAME'] . $imagePath)) {}

if (@getimagesize('http://' . $_SERVER['SERVER_NAME'] . $imagePath)) {}

and with

if (JFile::exists('http://' . $_SERVER['SERVER_NAME'] . $imagePath)){}

The two first ones don't work and return false even when image exists and the last one breaks the whole page.

How can I check if the K2 article image exist?

1

Most PHP file/image checking methods work on a local image.

If an image is stored in your site by a third-party extension, then you can also check it with a local path.

If an image is an external source and you have an Url, you have to download it to a temporary file, validate, return the status and delete the temporary file.

For example:

$imagefile = JInstallerHelper::downloadPackage($image_url);
list($width, $height, $type, $attr) = getimagesize($imagefile);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.