2

I am working on product import system with icecat. I have a little problem to resolve with the image. The problem is Icecat can propose different way to download an image like :

http://images.icecat.biz/img/gallery_mediums/myimage.jpg http://images.icecat.biz/img/norm/medium/myimage.jpg ... My problem is how to delete this part of element http://images.icecat.biz/img/norm/medium/ in this user case to just myimage.jpg

There my current function

public function getProductMediumImage() {
  $icecat_products_image_medium = HTML::sanitize($_POST['icecat_products_image_medium']);
  $product_image_medium = str_replace('http://images.icecat.biz/img/gallery_mediums', '', $icecat_products_image_medium);
  $product_image_medium = str_replace('http://images.icecat.biz/img/norm/medium/', '', $icecat_products_image_medium);


  if (!is_null($icecat_products_image_medium)) {
    $content = @file_get_contents($icecat_products_image_medium);
    file_put_contents($this->template->getDirectoryPathTemplateShopImages() . 'products/' . $product_image_medium, $content);
  }

  $product_image = 'products/' . $product_image_medium;

  return $product_image;
}

Thank you

yokogeri
  • 1,217
  • 2
  • 8
  • 11

5 Answers5

3

One and easiest way is to use basename() function:

$url = 'http://images.icecat.biz/img/gallery_mediums/myimage.jpg';
echo basename($url); // echoes myimage.jpg

You could also use pathinfo() which returns array one of which items is basename:

Array (
    [dirname] => http://images.icecat.biz/img/gallery_mediums 
    [basename] => myimage.jpg 
    [extension] => jpg 
    [filename] => myimage
)

And to get basename from there you do:

$url = 'http://images.icecat.biz/img/gallery_mediums/myimage.jpg';
$img = pathinfo($url)['basename'];
echo $img; // echoes myimgage.jpg
Roland Ruul
  • 1,172
  • 8
  • 15
2

You can use the php pathinfo() function to get information of a file system path. This will also work for a url.

$_POST['icecat_products_image_medium'] = 'http://images.icecat.biz/img/gallery_mediums/myimage.jpg';
$url = HTML::sanitize($_POST['icecat_products_image_medium']);
$info = pathinfo($url);
echo $info['basename']; // 'myimage.jpg'
var_export($info); //array (
//  'dirname' => 'http://images.icecat.biz/img/gallery_mediums',
//  'basename' => 'myimage.jpg',
//  'extension' => 'jpg',
//  'filename' => 'myimage'
//)
Xyz
  • 5,955
  • 5
  • 40
  • 58
1

parse_url() and pathinfo() works wonders

$file = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_BASENAME);
Xyz
  • 5,955
  • 5
  • 40
  • 58
Symeon Quimby
  • 820
  • 11
  • 17
1

Regex way:

$url = 'http://images.icecat.biz/img/gallery_mediums/whatever.jpg';
preg_match_all('#\/\K(?!.*\/).+$#mui', $url, $matches, PREG_SET_ORDER, 0);
echo $matches[0][0];
Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70
1

strrpos will give the last occurrence of a character, and then you can get the actual image filename.

$url = 'http://images.icecat.biz/img/gallery_mediums/myimage.jpg';

echo substr($url, (strrpos ($url, '/') + 1));
A R
  • 1,412
  • 2
  • 14
  • 29
Sivanag
  • 11
  • 2