/** * Store logo image and name into database * @param {integer} $id Club ID * @return {string} "" on success; else error */ function setLogo($id) { $this->myLogger->enter(); // el logo 1 NO es editable y debe ser siempre "rsce.png" if ($id <= 1) { return $this->error("Cannot change Logo for club ID:{$id}"); } // 1- leemos la imagen que nos viene en el post extrayendo el tipo y los datos $imgstr = http_request("imagedata", "s", null); if (!$imgstr) { return $this->error("No image data received for club ID:{$id}"); } if (!preg_match('/data:([^;]*);base64,(.*)/', $imgstr, $matches)) { return $this->error("Invalid received image string data:'{$imgstr}'"); } // $type=$matches[1]; // 'image/png' , 'image/jpeg', or whatever. Not really used $image = base64_decode(str_replace(' ', '+', $matches[2])); // also replace '+' to spaces or newlines $img = imagecreatefromstring($image); if (!$img) { return $this->error("Invalid received image string data:'{$imgstr}'"); } // 2- creamos una imagen de 150x150, le anyadimos canal alfa, y hacemos un copyresampled $newImage = imagecreatetruecolor(150, 150); imagealphablending($newImage, true); imagesavealpha($newImage, true); // Allocate a transparent color and fill the new image with it. // Without this the image will have a black background instead of being transparent. $transparent = imagecolorallocatealpha($newImage, 0, 0, 0, 127); imagefill($newImage, 0, 0, $transparent); imagecopyresampled($newImage, $img, 0, 0, 0, 0, 150, 150, imagesx($img), imagesy($img)); // 3- obtenemos el nombre del logo actual $row = $this->__selectObject("Nombre,Logo", "Clubes", "ID={$id}"); if (!$row) { return $this->error($this->conn->error); } $logo = $row->Logo; $name = $row->Nombre; // 4- si es igual al default, generamos un nuevo nombre para el logo basado en el nombre del club // y actualizamos el nombre en la bbdd if (Federations::logoMatches($logo)) { // compose logo file name based in club name, instead (old) club ID // Remove all (back)slashes from name $logo = str_replace('\\', '', $name); $logo = str_replace('/', '', $logo); // Remove all characters that are not the separator, a-z, 0-9, or whitespace $logo = preg_replace('![^' . preg_quote('-') . 'a-z0-_9\\s]+!', '', strtolower($logo)); // Replace all separator characters and whitespace by a single separator $logo = preg_replace('![' . preg_quote('-') . '\\s]+!u', '_', $logo); $logo = "{$logo}.png"; $sql = "UPDATE Clubes SET Logo='{$logo}' WHERE (ID={$id})"; $res = $this->query($sql); if (!$res) { return $this->error($this->conn->error); } } // 5- finalmente guardamos el logo en el fichero especificado en formato png $fname = __DIR__ . "/../../../images/logos/{$logo}"; // default path to store logos // $this->myLogger->info("Trying to save png file to:'$fname'"); imagepng($newImage, $fname); // seems that imagepng fails on save to file due to strange permission related issue // ob_start();// store output // imagePNG($newImage);// output to buffer // file_put_contents($fname, ob_get_contents(), FILE_BINARY);// write buffer to file // ob_end_clean();// clear and turn off buffer // 6- limpiamos y retornamos OK imagedestroy($img); imagedestroy($newImage); $this->myLogger->leave(); return ""; }