PHP(smatry)+Mysqlで画像投稿する際に画像の大きさ(横幅・縦幅)と容量を追加で保存させようと思い、他の
サイトにて質問したりgoogoleで検索して調べているのですが上手くいかず困っています。
CREATE TABLE IF NOT EXISTS `posts` ( `PID` bigint(20) NOT NULL AUTO_INCREMENT, `scriptolutiondesc` varchar(50) NOT NULL, `time_added` varchar(20) DEFAULT NULL, `date_added` date NOT NULL DEFAULT '0000-00-00', `active` char(1) NOT NULL DEFAULT '', `last_viewed` varchar(20) NOT NULL DEFAULT '', `ext` varchar(6) NOT NULL, `views` bigint(20) NOT NULL DEFAULT '0', `rating` float NOT NULL, `ratingcount` bigint(20) NOT NULL, `ip` varchar(20) NOT NULL, `size` int(11) NOT NULL, // 追加 `width` int(11) NOT NULL, // 追加 `height` int(11) NOT NULL, // 追加 PRIMARY KEY (`PID`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=60 ;
submit.php
ini_set('display_errors', 1); error_reporting(E_ALL); // 初期化 $error = null; $msg = null; $processgif = null; if($_REQUEST['subimage']!="") { $accept_tos = cleanit($_REQUEST['accept_tos']); $captcha = cleanit($_REQUEST['captcha']); $uploadedimage = $_FILES['postimage']['tmp_name']; $scriptolutiondesc = cleanit($_REQUEST['scriptolutiondesc']); // 追加 $size = $_FILES['postimage']['size']; // 画像サイズを取得 list($width, $height) = getimagesize($uploadedimage); if($scriptolutiondesc != "") { if(banned_words_chk($scriptolutiondesc)) { $error = $lang['51']; } } if($accept_tos != "1") { $error = $lang['15']; } elseif($captcha != $_SESSION['imagecode']) { $error = $lang['14']; } elseif($uploadedimage != "") { $record_datum = array( 'active' => "'".'0'."'", 'time_added' => "'".time()."'", 'date_added' => "'".date("Y-m-d")."'", 'ip' => "'".$_SERVER['REMOTE_ADDR']."'", 'size' => $size, 'width' => $width, 'height' => $height, ); if ($scriptolutiondesc != "") { $record_datum['scriptolutiondesc'] = "'".mysql_real_escape_string($scriptolutiondesc)."'"; } $query = sprintf("INSERT INTO posts (%s) VALUES (%s);", array_keys($record_datum), array_values($record_datum)); $result=$conn->execute($query); $id = mysql_insert_id(); $theimageinfo = getimagesize($uploadedimage); $thepp = $id; if($theimageinfo[2] == 1) { $thepp .= "-o.gif"; $thepp2 = ".gif"; $processgif = "1"; } elseif($theimageinfo[2] == 2) { $thepp .= "-o.jpg"; $thepp2 = ".jpg"; } elseif($theimageinfo[2] == 3) { $thepp .= "-o.png"; $thepp2 = ".png"; } else { $error = $lang['16']; } if($error == "") { $myvideoimgnew=$config['pdir']."/".$thepp; if(file_exists($myvideoimgnew)) { unlink($myvideoimgnew); } $myconvertimg = $_FILES['postimage']['tmp_name']; move_uploaded_file($myconvertimg, $myvideoimgnew); $tothumbdir = $config['pdir']."/".$id.$thepp2; if(file_exists($tothumbdir)) { unlink($tothumbdir); } if($processgif == "1") { do_resize_image2($myvideoimgnew, "800", "5000", true, $tothumbdir, $config['pdir']."/z1-".$thepp2); if(file_exists($config['pdir']."/z1-".$thepp2)) { unlink($config['pdir']."/z1-".$thepp2); } } else { do_resize_image($myvideoimgnew, "800", "800", true, $config['pdir']."/".$id.$thepp2, "1"); } $tothumbdirb = $config['pdir']."/".$id."-t".$thepp2; if(file_exists($tothumbdirb)) { unlink($tothumbdirb); } if($processgif == "1") { do_resize_image2($myvideoimgnew, "200", "800", true, $tothumbdirb, $config['pdir']."/z1-".$thepp2); if(file_exists($config['pdir']."/z1-".$thepp2)) { unlink($config['pdir']."/z1-".$thepp2); } } else { do_resize_image($myvideoimgnew, "200", "800", true, $config['pdir']."/".$id."-t".$thepp2, "0"); } $tothumbdirc = $config['pdir']."/".$id."-t2".$thepp2; if(file_exists($tothumbdirc)) { unlink($tothumbdirc); } if($processgif == "1") { do_resize_image2($myvideoimgnew, "100", "100", true, $tothumbdirc, $config['pdir']."/z1-".$thepp2); if(file_exists($config['pdir']."/z1-".$thepp2)) { unlink($config['pdir']."/z1-".$thepp2); } } else { do_resize_image($myvideoimgnew, "100", "100", false, $config['pdir']."/".$id."-t2".$thepp2, "0"); } if($config['wm'] == "1") { if($thepp2 != ".gif") { $watermark = $config['imagedir']."/".$config['watermark']; if($thepp2 == ".png") { $img=imagecreatefrompng($config['pdir']."/".$id.$thepp2); } else { $img=imagecreatefromjpeg($config['pdir']."/".$id.$thepp2); } $img_width=imagesx($img); $img_height=imagesy($img); $watermark=imagecreatefrompng($watermark); $watermark_width=imagesx($watermark); $watermark_height=imagesy($watermark); $image=imagecreatetruecolor($watermark_width, $watermark_height); imagealphablending($image, false); $dest_x=$img_width-$watermark_width-5; $dest_y=$img_height-$watermark_height-5; imagecopy($img, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height); imagesavealpha($img, true); imagejpeg($img, $config['pdir']."/".$id.$thepp2, 90); } } if(file_exists($config['pdir']."/".$thepp)) { if($config['approve_images'] == "0") { $active = ", active='1'"; } $query = "UPDATE posts SET ext='$thepp2' $active WHERE PID='".mysql_real_escape_string($id)."'"; $conn->execute($query); $msg = $lang['18']; } else { $error = $lang['17']; } } } } $pagetitle = $lang['0']; STemplate::assign('pagetitle',$pagetitle); //TEMPLATES BEGIN STemplate::assign('error',$error); STemplate::assign('msg',$msg); STemplate::display('header.tpl'); STemplate::display('submit.tpl'); STemplate::display('footer.tpl'); //TEMPLATES END ?>
miyabi-sun様とKosuke_Shibuya様のアドバイスを元に上記のように変更してみました。
ini_set('display_errors', 1); error_reporting(E_ALL);
上記を追加したことで複数「Notice: Undefined variable」が表示されたので5行目~7行目で変数の初期化を追加しました。
エラーも無くなったので、画像投稿を行ってもMysql側にデータは保存されません。(投稿も政情に完了したとメッセージが・・・)
これは、PHP側が悪いのでしょうか?それともMySQL側の設定が悪いのでしょうか?
PHP5.2.8
MySQL5.1.30
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/10 00:20