Aşağıdaki kodlarla sitedeki resmi watermark kullanarak kopyalıyorum. burada veri tabanında yaklaşık 1 milyon resim var ve bazıları veri tabanında kayıtlı olmasına rağmen dosya ve klasör yok sunucuda.
bu tür dosyalara geldiğinde aşağıdaki gibi hata alıyorum bunun kontrolünü nasıl yapabilirim.
Aldığım Hata: "Image source not readable"
Resimleri listeleme ve kopyalama Kodlar:
$resimlercek= DB::connection('mysql2')->table('files')
->where('product_id','=',$nesne_id)
->select('id','original_name','path')
->where('path', 'LIKE', '%png')
->where('path', 'LIKE', '%jpg')
->where('path', 'LIKE', '%gif')
->get();
foreach ($resimlercek as $resimcek) {
$bol = explode('.', $resimcek->original_name);
$value = str_slug($bol[0], '-', 'fr');
$guncelle = DB::connection('mysql2')->table('product_files')
->where('id', $resimcek->id)
->update(array(
"web_name" => $value . '.' . $bol[1]
));
$size = $this->getRemoteFileSize('http://sitename.com/storage/' . $dizin . '/' . $resimcek->path );
if ($size === null) {
} else {
if(file_exists('images/2/products/' . $value . '.' . $bol[1]))
{
}else{
$img = Image::make(public_path('storage/' . $dizin . '/' . $resimcek->path));
$img->insert(public_path('watermark.png'), 'bottom-right', 10, 10);
$img->save(public_path('images/2/products/' . $value . '.' . $bol[1]));
}
}
}
Resim olup olmadığını kontrol eden fonksiyon
public function getRemoteFileSize($url)
{
$ch = curl_init($url);
$headers = array(
'Range: bytes=0-1',
'Connection: close',
);
$in_headers = true;
$size = null;
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2450.0 Iron/46.0.2450.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0); // set to 1 to debug
curl_setopt($ch, CURLOPT_STDERR, fopen('php://output', 'r'));
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $line) use (&$in_headers, &$size) {
$length = strlen($line);
if (trim($line) == '') {
$in_headers = false;
}
list($header, $content) = explode(':', $line, 2);
$header = strtolower(trim($header));
if ($header == 'content-range') {
// found a content-range header
list($rng, $s) = explode('/', $content, 2);
$size = (int)$s;
return 0; // aborts transfer
} else if ($header == 'content-length' && 206 != curl_getinfo($curl, CURLINFO_HTTP_CODE)) {
// found content-length header and this is not a 206 Partial Content response (range response)
$size = (int)$content;
return 0;
} else {
// continue
return $length;
}
});
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($curl, $data) use ($in_headers) {
if (!$in_headers) {
// shouldn't be here unless we couldn't determine file size
// abort transfer
return 0;
}
// write function is also called when reading headers
return strlen($data);
});
$result = curl_exec($ch);
$info = curl_getinfo($ch);
return $size;
}