prova a mettere
prima di imagecreatefromjpeg() e controlla se $im non sia nullo. Stai attento all'uso degli header() che dovresti mettere solo se $im è una risorsa validaCodice PHP:
ini_set ('gd.jpeg_ignore_warning', 0);
Il precedente codice mi dà:Codice PHP:
$img_corr = "http://www.romaeurmeteo.it/foscam/FI9900P_00626E856BBD/snap/20180205/Schedule_20180205-083406.jpg";
$img_sana = "http://www.romaeurmeteo.it/foscam/FI9900P_00626E856BBD/snap/20180206/Schedule_20180206-005506.jpg";
ini_set ('gd.jpeg_ignore_warning', 0);
echo imagecreatefromjpeg($img_corr);
echo "<br>";
echo imagecreatefromjpeg($img_sana);
Resource id #3
Resource id #5
pare che per lui non ci siano problemi...
se metti all'inizio
error_reporting(E_ALL);
Ti mostra almeno un avviso?
Adesso sì! ho aggiunto la riga che mi hai detto all'inizio del codice che ho postato nel precedente post ed il risultato è questo:
Notice: imagecreatefromjpeg(): gd-jpeg, libjpeg: recoverable error: Corrupt JPEG data: premature end of data segment in D:\inetpub\webs\romaeurmeteoit\foscam\FI9900P_0062 6E856BBD\snap\getim.php on line 10
Resource id #3
Resource id #5
Adesso dovrei riuscire a sfruttare questa cosa per dare un comando del tipo:
if (ottengo questo warning) allora sposta l'immagine in cartella
si può fare??
Codice PHP:
error_reporting(E_ALL);
set_error_handler("noticeHandler", E_NOTICE);
function noticeHandler($errno, $errstr){
throw new Exception($errstr);
}
try{
...
$im = imagecreatefromjpeg(...);
...
}catch(Exception $ex){
echo $ex->getMessage();
}
php non ha un modo di "catturare" un notice e visto che imagecreatefromjpeg() ignora il notice (prosegue lo stesso) devi crearti un handler: con set_error_handler dici a php che quando capita un NOTICE lancia la funzione (in questo caso noticehandler).
Questa semplicemente crea (new ) e lancia (throw, "lancia" alla funzione chiamante) un'eccezione.
Per intercettare l'eccezione si usa il costrutto try...catch: Quando il codice dentro il try{} lancia un eccezione, viene "catturata" ed eseguito il codice dentro a catch().
Più o meno è questo che succede...ti invito a guardarti le eccezioni, perchè ci sono altre cose dovresti sapere (ad esempio catturare diversi tipi di eccezioni)
Più o meno ci siamo. Ma ho ancora un problema con questo codice:
Praticamente una immagine corrotta la sposta, ma poi dà questo errore:Codice PHP:
error_reporting(E_ALL);
set_error_handler("noticeHandler", E_WARNING);
function noticeHandler($errno, $errstr){
throw new Exception($errstr);
}
....
$fileCount = count($directory);
$index = 0;
for($j=0;$j<($j+2);$j++){
if($index == ($fileCount-1)){
break;
}
try{
while($index<$fileCount){
$im = imagecreatefromjpeg("20180125/".$directory[$index]);
$index++;
}
}
catch(Exception $e){
rename("20180125/".$directory[$index], "corrotte/".$directory[$index]);
}
}
Fatal error: Uncaught exception 'Exception' with message 'rename(20180125/Schedule_20180125-135206.jpg,corrotte/Schedule_20180125-135206.jpg): The system cannot find the file specified. (code: 2)' in D:\inetpub\webs\romaeurmeteoit\foscam\FI9900P_0062 6E856BBD\snap\incomplete.php:5 Stack trace: #0 [internal function]: noticeHandler(2, 'rename(20180125...', 'D:\\inetpub\\webs...', 43, Array) #1 D:\inetpub\webs\romaeurmeteoit\foscam\FI9900P_0062 6E856BBD\snap\incomplete.php(43): rename('20180125/Schedu...', 'corrotte/Schedu...') #2 {main} thrown in D:\inetpub\webs\romaeurmeteoit\foscam\FI9900P_0062 6E856BBD\snap\incomplete.php on line 5
(la linea 5 è: throw new Exception($errstr) )
A me sembra che l'errore sia:
The system cannot find the file specified. (code: 2)
Probabilmente, a causa di questo, la rename da a sua volta un notice che viene gestito dalla funzione noticeHandler che lancia l'eccezione, ma visto che non stai dentro un try non è gestita ed hai un errore.
- Puoi rimettere un nuovo blocco try...catch dentro al catch(Exception $e)
- Puoi mettere un nuovo blocco try..catch che comprende quello di adesso
- puoi sopprimere il notice con la @: @rename(...)
Tuttavia credo che il problema che non trova il file resta