Skip to content

Instantly share code, notes, and snippets.

@arudmin
Last active November 25, 2018 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arudmin/a803d3e975662753c6745252cb785d69 to your computer and use it in GitHub Desktop.
Save arudmin/a803d3e975662753c6745252cb785d69 to your computer and use it in GitHub Desktop.
allows to import images as URL to opencart
// catalog/model/tool/image.php
if (!is_file(DIR_IMAGE . $filename)) {
if (filter_var($filename, FILTER_VALIDATE_URL)) {
// generating dirname as first digit of productID (from filename) // PHP_EOL
preg_match_all('/_(\d{4,6})/m', basename($filename), $matches, PREG_SET_ORDER, 0);
$image_dir = $matches[0][1] ? $matches[0][1][0] : 'temp';
$image_name = $matches[0][1] ? basename($filename) : substr(base64_encode($filename), 0, 7);
$image_path = 'catalog/' . $image_dir . '/' . $image_name;
if (!is_file(DIR_IMAGE . $image_path)) {
if (!is_dir(dirname(DIR_IMAGE . $image_path))){
@mkdir(dirname(DIR_IMAGE . $image_path), 0700);
}
$filename = file_put_contents(DIR_IMAGE . $image_path, file_get_contents($filename)) ? $image_path : 'no_image.jpg';
} else {
$filename = $image_path;
}
} else {
if (is_file(DIR_IMAGE . 'no_image.jpg')) {
$filename = 'no_image.jpg';
} elseif (is_file(DIR_IMAGE . 'no_image.png')) {
$filename = 'no_image.png';
} else {
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment