Advertisement
nicolaslagios

PHP - Save Images from an array and rename them

Jan 7th, 2023
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. //Ask me for details --> Nicolaslagios.com
  2. foreach ($telikospinakas as $row) {
  3.     if ($row['Parent SKU'] == 0) {
  4.         $imagearray = explode("|", $row['Image URL']);
  5.         $count = 1;
  6.         foreach ($imagearray as $image) {
  7.             // Check if image URL is valid
  8.             if(@getimagesize($image)){
  9.                 // Save the image to the "images" folder
  10.                 $image_name = $row['SKU'];
  11.                 if($count > 1){
  12.                     $image_name .= "_" . chr(96 + $count);
  13.                 }
  14.                 // Get the extension of the original image file
  15.                 $path_parts = pathinfo($image);
  16.                 $extension = $path_parts['extension'];
  17.                 // Save the image with the original extension
  18.                 file_put_contents("images/" . $image_name . "." . $extension, file_get_contents($image));
  19.             }
  20.             $count++;
  21.         }
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement